package com.caimei.www.service.page.impl;

import com.caimei.www.mapper.EquipmentDao;
import com.caimei.www.mapper.SinglePageDao;
import com.caimei.www.pojo.JsonModel;
import com.caimei.www.pojo.page.ImageLink;
import com.caimei.www.pojo.page.PageContent;
import com.caimei.www.pojo.page.PageFloor;
import com.caimei.www.pojo.page.Parameter;
import com.caimei.www.service.page.EquipmentService;
import com.caimei.www.utils.ImageUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * Description
 *
 * @author : Charles
 * @date : 2020/7/16
 */
@Slf4j
@Service
public class EquipmentServiceImpl implements EquipmentService {
    @Resource
    private EquipmentDao equipmentDao;
    @Resource
    SinglePageDao singlePageDao;

    @Value("${caimei.wwwDomain}")
    private String domain;

    /**
     * 项目仪器详情
     *
     * @param equipmentId
     * @return
     */
    @Override
    public PageContent getEquipmentById(Integer equipmentId) {
        PageContent equipment = equipmentDao.getEquipmentById(equipmentId);
        List<Parameter> contents = equipmentDao.getParametersByEquipmentId(equipmentId);
        equipment.setPageContents(contents);
        return equipment;
    }

    /**
     * 获取项目仪器详情页中层信息(搭配推荐,相似商品)
     *
     * @param equipmentId
     * @return
     */
    @Override
    public JsonModel<List<PageFloor>> getEquipmentRecommendById(Integer equipmentId) {
        if (equipmentId == null) { return JsonModel.error("参数异常", null);}
        List<PageFloor> floorList = singlePageDao.getFloorByPageId(equipmentId);
        if (floorList.size() > 0) {
            List<PageFloor> tempList = new ArrayList<>();
            floorList.forEach(floor -> {
                List<ImageLink> imageLinks = singlePageDao.getDataByFloorId(floor.getId());
                if (imageLinks.size() > 0) {
                    imageLinks.forEach(img -> {
                        img.setImage(ImageUtil.getImageURL("actType", img.getImage(), 0, domain));
                    });
                    floor.setFloorData(imageLinks);
                } else {
                    tempList.add(floor);
                }
            });
            if(tempList.size() > 0){
                floorList.removeAll(tempList);
            }
        }
        return JsonModel.success(floorList);
    }
}
