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.SeedSelfInspectionDao;
import com.sw.mes.yb.seed.dto.SeedIQCInspectionDto;
import com.sw.mes.yb.seed.dto.SeedSelfInspectionDto;
import com.sw.mes.yb.seed.entity.SeedData;
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 SeedSelfInspectionServiceImpl extends BaseServiceImpl<SeedSelfInspection> implements SeedSelfInspectionService {
    private SeedSelfInspectionDao dao;
    @Autowired
    private SeedIQCInspectionService seedIQCInspectionService;
    @Autowired
    private SeedService seedService;
    @Autowired
    private DictionaryItemService dictionaryItemService;

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

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

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

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

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

    @Override
    public List<SeedSelfInspection> getByBatchNo(String batchNo) {
        return dao.getByBatchNo(batchNo);
    }

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

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

    public SeedSelfInspection fromDto(SeedSelfInspectionDto dto) {
        SeedSelfInspection seedSelfInspection = new SeedSelfInspection();
        BeanUtils.copyProperties(dto, seedSelfInspection);
        return seedSelfInspection;
    }
}
