package com.caimei.www.service.impl;

import com.caimei.www.mapper.InstrumentDao;
import com.caimei.www.mapper.SinglePageDao;
import com.caimei.www.pojo.JsonModel;
import com.caimei.www.pojo.content.ImageLink;
import com.caimei.www.pojo.content.PageContent;
import com.caimei.www.pojo.content.PageFloor;
import com.caimei.www.pojo.content.Parameter;
import com.caimei.www.service.InstrumentService;
import com.caimei.www.utils.ImageUtil;
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
 */
@Service
public class InstrumentServiceImpl implements InstrumentService {
    @Resource
    private InstrumentDao instrumentDao;
    @Resource
    SinglePageDao singlePageDao;

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

    /**
     * 项目仪器详情
     *
     * @param instrumentId
     * @return
     */
    @Override
    public PageContent getInstrumentById(Integer instrumentId) {
        PageContent instrument = instrumentDao.getInstrumentById(instrumentId);
        List<Parameter> contents = instrumentDao.getParametersByInstrumentId(instrumentId);
        instrument.setPageContents(contents);
        return instrument;
    }

    /**
     * 获取项目仪器详情页中层信息(搭配推荐,相似商品)
     *
     * @param instrumentId
     * @return
     */
    @Override
    public JsonModel<List<PageFloor>> getInstrumentRecommendById(Integer instrumentId) {
        if (instrumentId == null) { return JsonModel.error("参数异常", null);}
        List<PageFloor> floorList = singlePageDao.getFloorByPageId(instrumentId);
        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);
    }
}
