package com.sw.mes.itemclean.clean.service.impl;

import com.sw.mes.core.api.system.YbFeignClients;
import com.sw.mes.core.context.BaseContextHandler;
import com.sw.mes.core.enums.ItemCleanEnums;
import com.sw.mes.core.response.Result;
import com.sw.mes.core.response.ResultCode;
import com.sw.mes.core.response.ResultGenerator;
import com.sw.mes.itemclean.clean.domain.MaterialLandingMaterialSignature;
import com.sw.mes.itemclean.clean.domain.MaterialStoreSignBox;
import com.sw.mes.itemclean.clean.dto.batchNoOperate.BatchNoOperate;
import com.sw.mes.itemclean.clean.dto.boxNoOperate.BatchNo;
import com.sw.mes.itemclean.clean.dto.boxNoOperate.BoxNoOperate;
import com.sw.mes.itemclean.clean.dto.MaterialLandingMaterialSignatureDto;
import com.sw.mes.itemclean.clean.mapper.MaterialLandingMaterialSignatureMapper;
import com.sw.mes.itemclean.clean.service.GenerateBusinessNoService;
import com.sw.mes.itemclean.clean.service.MaterialLandingMaterialSignatureService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sw.mes.itemclean.clean.service.MaterialStoreSignBoxService;
import org.modelmapper.ModelMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;

/**
 * <p>
 * 落地料打签 服务实现类
 * </p>
 *
 * @author liuzemin
 * @since 2022-11-10
 */
@Service
public class MaterialLandingMaterialSignatureServiceImpl extends ServiceImpl<MaterialLandingMaterialSignatureMapper, MaterialLandingMaterialSignature> implements MaterialLandingMaterialSignatureService {


    @Autowired
    GenerateBusinessNoService generateBusinessNoService;

    @Autowired
    ModelMapper modelMapper;

    @Autowired
    MaterialStoreSignBoxService materialStoreSignBoxService;

    @Autowired
    private YbFeignClients ybFeignClients;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result landingMaterialSignature(MaterialLandingMaterialSignatureDto materialLandingMaterialSignatureDto) {
        // 生成箱号、批次号
        BatchNoOperate batchNoOperate = new BatchNoOperate(ItemCleanEnums.BatchType.落地料, ItemCleanEnums.MaterialResource.getEnum(materialLandingMaterialSignatureDto.getMaterialResource()), materialLandingMaterialSignatureDto.getMaterialCode());
        String productBatchNo = generateBusinessNoService.generateProductBatchNo(batchNoOperate);
        String materialBoxNo= generateBusinessNoService.generateBoxNo(new BoxNoOperate(ItemCleanEnums.BoxNoType.默认,new BatchNo(productBatchNo)));
        if (productBatchNo.isEmpty() || materialBoxNo.isEmpty()){
            throw new RuntimeException("打签失败！");
        }
        materialLandingMaterialSignatureDto.setMaterialBoxNo(materialBoxNo);
        materialLandingMaterialSignatureDto.setProductBatchNo(productBatchNo);
        materialLandingMaterialSignatureDto.setCreateUser(BaseContextHandler.getUsername());
        materialLandingMaterialSignatureDto.setCreateTime(LocalDateTime.now());
        materialLandingMaterialSignatureDto.setDelFlag(0);
        //保存落地料打签记录
        MaterialLandingMaterialSignature materialLandingMaterialSignature = fromDto(materialLandingMaterialSignatureDto);
        this.saveOrUpdate(materialLandingMaterialSignature);

        // 打签箱暂存表
        MaterialStoreSignBox materialStoreSignBox = toMaterialStoreSignBox(materialLandingMaterialSignatureDto);
        materialStoreSignBoxService.save(materialStoreSignBox);
        return ResultGenerator.success(materialBoxNo);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result findByMaterialCleanName(String name) {
        Result result = ybFeignClients.findByName(name);
        if (!result.getCode().equals(ResultCode.SUCCESS.code)) {
            throw new RuntimeException(result.getMessage());
        }
        if (result.getData() == null) {
            throw new RuntimeException("不存在"+name+"原料清洗产品模型！");
        }
        return ResultGenerator.success(result.getData());
    }

    public MaterialStoreSignBox toMaterialStoreSignBox(MaterialLandingMaterialSignatureDto materialLandingMaterialSignatureDto){
        MaterialStoreSignBox materialStoreSignBox = modelMapper.map(materialLandingMaterialSignatureDto, MaterialStoreSignBox.class);
        materialStoreSignBox.setMaterialType(3);
        materialStoreSignBox.setPickingStatus(0);
        materialStoreSignBox.setDelFlag(0);
        materialStoreSignBox.setMaterialResource(materialLandingMaterialSignatureDto.getMaterialResource());
        return materialStoreSignBox;
    }

    public MaterialLandingMaterialSignature fromDto(MaterialLandingMaterialSignatureDto dto) {
        return modelMapper.map(dto, MaterialLandingMaterialSignature.class);
    }
}
