package com.sw.common.accessory.server.service.impl;

import com.sw.common.accessory.server.EnumValuesAccy;
import com.sw.common.accessory.server.dto.DeterminationEchnologyDto;
import com.sw.common.accessory.server.entity.*;
import com.sw.common.accessory.server.mapper.DeterminationEchnologyMapper;
import com.sw.common.accessory.server.service.*;
import com.sw.common.core.context.BaseContextHandler;
import com.sw.common.core.response.ResultGenerator;
import com.sw.common.mybatisplus.base.BaseServiceImpl;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.sw.common.core.response.Result;

import java.util.Date;

@Service
@Transactional
public class DeterminationEchnologyServiceImpl extends BaseServiceImpl<DeterminationEchnologyMapper, DeterminationEchnology> implements DeterminationEchnologyService {
    @Autowired
    private DeterminationEchnologyMapper mapper;
    @Autowired
    @Lazy
    private AccessoryPartDetailsService accessoryPartDetailsService;
    @Autowired
    @Lazy
    private DeterminationQualityService qualityService;
    @Autowired
    @Lazy
    private AccessoryService accessoryService;
    @Override
    public DeterminationEchnology findEchnologyByUniqueCode(String uniqueCode) {
        return mapper.findEchnologyByUniqueCode(uniqueCode);
    }

    @Override
    public DeterminationEchnology findEchnologyByAccessoryId(Long accessoryId) {
        return mapper.findEchnologyByAccessoryId(accessoryId);
    }

    @Override
    public DeterminationEchnologyDto findById(Long id) {
        DeterminationEchnology determinationEchnology = mapper.selectById(id);
        DeterminationEchnologyDto eagerDto = toEagerDto(determinationEchnology);
        String equipmentCode = "";
        String materialCode = "";
        if (StringUtils.isNotBlank(determinationEchnology.getEquipmentCode())) {
            equipmentCode = determinationEchnology.getEquipmentCode();
        }
        if (StringUtils.isNotBlank(determinationEchnology.getMaterialCode())) {
            materialCode = determinationEchnology.getMaterialCode();
        }
        AccessoryPartDetails accessoryPartDetails = accessoryPartDetailsService.getByDeviceCodeAndMaterialCode(equipmentCode,materialCode);
        if(ObjectUtils.isNotEmpty(accessoryPartDetails)){
            eagerDto.setPartName(accessoryPartDetails.getPartName());
        }
        return eagerDto;
    }

    @Override
    public DeterminationEchnology getEchnologyById(Long id) {
        return mapper.getEchnologyById(id);
    }

    @Override
    public Result confirmAudit(DeterminationEchnologyDto dto, DeterminationEchnology entity) {
        BeanUtils.copyProperties(dto,entity);
        entity.setAuditPass(true);
        entity.setStatus(1);
        entity.setDel(true);
        entity.setUpdateBy(BaseContextHandler.getUsername());
        entity.setUpdateTime(new Date());
        int update = mapper.updateById(entity);
        if(update>=1){
            DeterminationQuality determinationQuality = new DeterminationQuality();
            determinationQuality.setUniqueCode(entity.getUniqueCode());
            determinationQuality.setAccessoryId(entity.getAccessoryId());
            determinationQuality.setMaterialAnomaly(entity.isMaterialAnomaly());
            determinationQuality.setAuditPass(false);
            determinationQuality.setCreateBy(BaseContextHandler.getUsername());
            determinationQuality.setCreateTime(new Date());
            determinationQuality.setEchnologyId(entity.getId());
            determinationQuality.setEquipmentCode(entity.getEquipmentCode());
            determinationQuality.setMaterialCode(entity.getMaterialCode());
            determinationQuality.setMaterialName(entity.getMaterialName());
            determinationQuality.setStatus(0);
            determinationQuality.setResultDetermination(entity.getResultDetermination());
            determinationQuality.setAbnormalCondition(entity.getAbnormalCondition());
            qualityService.save(determinationQuality);
            Accessory accessory = accessoryService.get(entity.getAccessoryId());
            switch (dto.getResultDetermination()){
                case "报废":
                    accessory.setAccessoryStatus(EnumValuesAccy.AccessoryStatus.报废.getKey());
                    break;
                case "返修":
                    accessory.setAccessoryStatus(EnumValuesAccy.AccessoryStatus.返修.getKey());
                    break;
                default:
                    accessory.setAccessoryStatus(EnumValuesAccy.AccessoryStatus.煅烧可用.getKey());
            }
            accessoryService.saveOrUpdate(accessory);
        }
        return ResultGenerator.success();
    }

    @Override
    public Result unConfirmAudit(DeterminationEchnologyDto dto, DeterminationEchnology entity) {
        BeanUtils.copyProperties(dto,entity);
        entity.setAuditPass(false);
        entity.setStatus(2);
        entity.setDel(true);
        entity.setUpdateBy(BaseContextHandler.getUsername());
        entity.setUpdateTime(new Date());
        int update = mapper.updateById(entity);
        if(update >= 1){
            Accessory accessory = accessoryService.get(entity.getAccessoryId());
            accessory.setStatus(EnumValuesAccy.AccyUseStatus.待上机.getKey());
            accessory.setAccessoryStatus(0);
            accessoryService.saveOrUpdate(accessory);
        }
        return ResultGenerator.success();
    }

    @Override
    public DeterminationEchnology findByInputBillNo(String billNo) {
        DeterminationEchnology byInputBillNo = mapper.findByInputBillNo(billNo);
        return byInputBillNo;
    }

    public DeterminationEchnologyDto toLazyDto(DeterminationEchnology entity) {
        DeterminationEchnologyDto determinationEchnologyDto = new DeterminationEchnologyDto();
        BeanUtils.copyProperties(entity, determinationEchnologyDto);
        return determinationEchnologyDto;
    }

    public DeterminationEchnologyDto toEagerDto(DeterminationEchnology entity) {
        DeterminationEchnologyDto determinationEchnologyDto = new DeterminationEchnologyDto();
        BeanUtils.copyProperties(entity, determinationEchnologyDto);
        return determinationEchnologyDto;
    }

    public DeterminationEchnology fromDto(DeterminationEchnology dto) {
        DeterminationEchnology determinationEchnology = new DeterminationEchnology();
        BeanUtils.copyProperties(dto, determinationEchnology);
        return determinationEchnology;
    }


}
