package com.sw.mes.yb.seed.service.impl;

import com.sw.mes.base.system.entity.DictionaryItem;
import com.sw.mes.base.system.service.DictionaryItemService;
import com.sw.mes.core.base.BaseServiceImpl;
import com.sw.mes.core.context.BaseContextHandler;
import com.sw.mes.core.response.Result;
import com.sw.mes.core.response.ResultGenerator;
import com.sw.mes.yb.seed.dao.SeedIQCInspectionDao;
import com.sw.mes.yb.seed.dto.SeedIQCInspectionDto;
import com.sw.mes.yb.seed.entity.SeedData;
import com.sw.mes.yb.seed.entity.SeedIQCInspection;
import com.sw.mes.yb.seed.entity.SeedSelfInspection;
import com.sw.mes.yb.seed.service.SeedIQCInspectionService;
import com.sw.mes.yb.seed.service.SeedSelfInspectionService;
import com.sw.mes.yb.seed.service.SeedService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.text.DecimalFormat;
import java.util.Date;
import java.util.List;
import java.util.Optional;

@Service
public class SeedIQCInspectionServiceImpl extends BaseServiceImpl<SeedIQCInspection> implements SeedIQCInspectionService {
    private SeedIQCInspectionDao dao;
    @Autowired
    private SeedService seedService;
    @Autowired
    private DictionaryItemService dictionaryItemService;

    @Autowired
    public void setDao(SeedIQCInspectionDao dao) {
        this.dao = dao;
        super.setBaseDao(dao);
    }

    @Override
    public SeedIQCInspection getBySeedCode(String seedCode) {
        return dao.getBySeedCode(seedCode);
    }

    @Override
    public List<SeedIQCInspection> getIQCInspectionBySeedCodes(List<String> seedCodes) {
        return dao.getIQCInspectionBySeedCodes(seedCodes);
    }

    @Override
    public SeedIQCInspection getBySeedBatchNo(String seedBatchNo) {
        return dao.getBySeedBatchNo(seedBatchNo);
    }

    @Override
    public List<SeedIQCInspection> getByPlanCode(String planCode) {
        return dao.getByPlanCode(planCode);
    }

    @Override
    public List<SeedIQCInspection> getByBatchNo(String batchNo, Boolean isQualified) {
        return dao.getByBatchNo(batchNo, isQualified);
    }

    @Override
    public List<SeedIQCInspection> getQualified(Boolean isQualified) {
        return dao.getQualified(isQualified);
    }

    @Override
    public Result create(SeedIQCInspectionDto dto) {
        SeedIQCInspection seedCode = dao.getBySeedCode(dto.getSeedCode());
        SeedData seedData = seedService.findByCode(dto.getSeedCode());
        SeedIQCInspection seedIQCInspection = new SeedIQCInspection();
        BeanUtils.copyProperties(dto, seedIQCInspection);
        List<DictionaryItem> zjllzlxs = dictionaryItemService.findItemByThemeValue("zjllzlxs", true);
        if (!ObjectUtils.isEmpty(zjllzlxs)) {
            DecimalFormat decimalFormat = new DecimalFormat("0.##");
            String totalLength = decimalFormat.format(dto.getTotalLength());
            Optional<DictionaryItem> weights = zjllzlxs.stream().filter(weight -> totalLength.contains(weight.getName())).findFirst();
            if (weights.isPresent()) {
                //理论重量系数
                seedIQCInspection.setTheWeight(weights.get().getValue());
            }
        }
        if (!ObjectUtils.isEmpty(seedCode)) {
            seedIQCInspection.setId(seedCode.getId());
            seedIQCInspection.setUpdateBy(BaseContextHandler.getUsername());
            seedIQCInspection.setUpdateDate(new Date());
        }
        if (!ObjectUtils.isEmpty(seedData)) {
            seedIQCInspection.setPlanCode(seedData.getPlanCode());
            seedIQCInspection.setBatchNo(seedData.getBatchNo());
            seedIQCInspection.setSeedBatchNo(seedData.getSeedBatchNo());
            seedIQCInspection.setMaterialId(seedData.getMaterialId());
            seedIQCInspection.setMaterialCode(seedData.getMaterialCode());
            seedIQCInspection.setMaterialType(seedData.getMaterialType());
            seedIQCInspection.setMaterialModel(seedData.getMaterialModel());
            seedIQCInspection.setMaterialName(seedData.getMaterialName());
            seedIQCInspection.setSpec(seedData.getSpec());
        }
        seedIQCInspection.setCreateBy(BaseContextHandler.getUsername());
        seedIQCInspection.setCreateDate(new Date());
        this.saveOrUpdate(seedIQCInspection);
        if(seedIQCInspection.getIsQualified() == true){
            seedData.setIqcQualified(true);
            seedData.setDetectionTime(seedIQCInspection.getCreateDate());
            seedService.saveOrUpdate(seedData);
        }else {
            seedData.setIqcQualified(false);
            seedData.setDetectionTime(seedIQCInspection.getCreateDate());
            seedService.saveOrUpdate(seedData);
        }
        return ResultGenerator.success();
    }

}
