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

import com.sw.mes.base.factory.material.service.UnitService;
import com.sw.mes.base.factory.product.entity.ProductModel;
import com.sw.mes.base.factory.product.service.ProductModelService;
import com.sw.mes.core.api.system.StoreFeignClients;
import com.sw.mes.core.api.system.YbFeignClients;
import com.sw.mes.core.context.BaseContextHandler;
import com.sw.mes.core.dto.ProductionOrderDto;
import com.sw.mes.core.dto.wms.MesasnDto;
import com.sw.mes.core.dto.wms.MesasnOrderDto;
import com.sw.mes.core.enums.SeedStoreStatusEnum;
import com.sw.mes.core.response.Result;
import com.sw.mes.core.response.ResultGenerator;
import com.sw.mes.yb.seed.api.service.consumer.SeedConsumerService;
import com.sw.mes.yb.seed.dto.SeedGroupBracketDetailsDto;
import com.sw.mes.yb.seed.dto.SeedGroupBracketDto;
import com.sw.mes.yb.seed.entity.SeedGroupBracket;
import com.sw.mes.yb.seed.entity.SeedGroupBracketDetails;
import com.sw.mes.yb.seed.enums.InputBillStatusEnum;
import com.sw.mes.yb.seed.service.SeedGroupBracketDetailsService;
import com.sw.mes.yb.seed.service.SeedGroupBracketService;
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.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class SeedConsumerServiceImpl implements SeedConsumerService {
    @Autowired
    private SeedGroupBracketService seedGroupBracketService;
    @Autowired
    private SeedGroupBracketDetailsService seedGroupBracketDetailsService;
    @Autowired
    private ProductModelService productModelService;
    @Autowired
    private UnitService unitService;
    @Autowired
    private StoreFeignClients storeFeignClients;
    @Autowired
    private YbFeignClients ybFeignClients;

    @Override
    public Result wmsInitiate(Long id) {
        //查询组托数据
        SeedGroupBracketDto seedGroupBracketDto = seedGroupBracketService.findByIds(id);
        List<MesasnDto> mesasnDtos = new ArrayList<>();
        if (!ObjectUtils.isEmpty(seedGroupBracketDto) && seedGroupBracketDto.getStatus().equals(SeedStoreStatusEnum.待出库.getName())) {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            //查询明细数据
            List<SeedGroupBracketDetailsDto> seedGroupBracketDetails = seedGroupBracketDto.getDetailsDtos();
            //查询对应的主数据
            ProductModel productModel = productModelService.findByCode(seedGroupBracketDto.getMaterialCode());
            //组装对应数据
            MesasnDto mesasnDto = new MesasnDto();
            mesasnDto.setOrganizationid("JINKO");
            mesasnDto.setWarehouseid("5041");
            mesasnDto.setCustomerid("5041");
            mesasnDto.setDocno(seedGroupBracketDto.getInputBillCode());
            mesasnDto.setAsntype("CLCPRK");
            mesasnDto.setAsnstatus("00");
            mesasnDto.setAsnreferenced("籽晶组托入库");
            mesasnDto.setAsnreferencea(seedGroupBracketDto.getId().toString());
            mesasnDto.setAsnreferenceb("5041");//车间编号
            mesasnDto.setAsnreferencec("P32B");
            mesasnDto.setAsncreationtime(sdf.format(seedGroupBracketDto.getCreateDate()));
            mesasnDto.setTraceid(seedGroupBracketDto.getCode());//托盘号
            mesasnDto.setSerialno(seedGroupBracketDto.getCode());//箱号
            mesasnDto.setSku(seedGroupBracketDto.getMaterialCode());//物料编码
            mesasnDto.setExpectedqty(String.valueOf(seedGroupBracketDetails.size()));//组件数量
            mesasnDto.setLotatt01(sdf.format(new Date()));
            mesasnDto.setLotatt04(seedGroupBracketDto.getSeedDayBatchNo());//入库批次
            mesasnDto.setLotatt08("N");//质量状态
            mesasnDto.setLotatt09("5041");//厂家
            mesasnDto.setLotatt10("5041");//供应商
            mesasnDto.setLotatt12(seedGroupBracketDto.getSeedDayBatchNo());//生产批次
            if (!ObjectUtils.isEmpty(productModel)) {
                mesasnDto.setLotatt19(productModel.getResistivity());//电阻率
                mesasnDto.setLotatt20(productModel.getProductGrade());//等级
            }

            mesasnDto.setCreatesource("MES");
            List<MesasnOrderDto> mesasnOrderDtos = new ArrayList<>();
            MesasnOrderDto mesasnOrderDto = new MesasnOrderDto();
            mesasnOrderDto.setSku(seedGroupBracketDto.getMaterialCode());
            mesasnOrderDto.setTraceid(seedGroupBracketDto.getCode());
            mesasnOrderDto.setSerialno(seedGroupBracketDto.getCode());
            mesasnOrderDto.setExpectedqty(String.valueOf(seedGroupBracketDetails.size()));//箱重量
            mesasnOrderDto.setLotatt01(sdf.format(new Date()));
            mesasnOrderDto.setLotatt03(sdf.format(new Date()));
            mesasnOrderDto.setLotatt04(seedGroupBracketDto.getSeedDayBatchNo());//入库批次
            mesasnOrderDto.setLotatt05("5041");
            mesasnOrderDto.setLotatt08("N");//质量状态
            mesasnOrderDto.setLotatt09("5041");//厂家
            mesasnOrderDto.setLotatt12(seedGroupBracketDto.getSeedDayBatchNo());//生产批次
            mesasnOrderDto.setLotatt19(productModel.getResistivity());//电阻率
            mesasnOrderDto.setLotatt20(productModel.getProductGrade());//等级
            mesasnOrderDto.setDedi06("0");
            mesasnOrderDto.setDedi07(String.valueOf(seedGroupBracketDetails.get(0).getTotalLength()));//长度,整箱长度
            mesasnOrderDto.setDedi08(seedGroupBracketDto.getSeedDayBatchNo());//批号,MES下线批
            mesasnOrderDtos.add(mesasnOrderDto);
//            seedGroupBracketDetails.stream().forEach(detais -> {
//                MesasnOrderDto mesasnOrderDto = new MesasnOrderDto();
//                mesasnOrderDto.setSku(seedGroupBracketDto.getMaterialCode());
//                mesasnOrderDto.setTraceid(seedGroupBracketDto.getCode());
//                mesasnOrderDto.setSerialno(seedGroupBracketDto.getCode());
//                mesasnOrderDto.setExpectedqty("1");//箱重量
//                mesasnOrderDto.setLotatt01(sdf.format(new Date()));
//                mesasnOrderDto.setLotatt03(sdf.format(new Date()));
//                mesasnOrderDto.setLotatt04(seedGroupBracketDto.getSeedDayBatchNo());//入库批次
//                mesasnOrderDto.setLotatt05("5041");
//                mesasnOrderDto.setLotatt08("N");//质量状态
//                mesasnOrderDto.setLotatt09("5041");//厂家
//                mesasnOrderDto.setLotatt12(seedGroupBracketDto.getSeedDayBatchNo());//生产批次
//                mesasnOrderDto.setLotatt19(productModel.getResistivity());//电阻率
//                mesasnOrderDto.setLotatt20(productModel.getProductGrade());//等级
//                mesasnOrderDto.setDedi06("0");
//                mesasnOrderDto.setDedi07(String.valueOf(detais.getTotalLength()));//长度,整箱长度
//                mesasnOrderDto.setDedi08(seedGroupBracketDto.getSeedDayBatchNo());//批号,MES下线批
//                mesasnOrderDtos.add(mesasnOrderDto);
//            });
            mesasnDto.setOrderList(mesasnOrderDtos);
            mesasnDtos.add(mesasnDto);
        }
        //发起WMS入库
        Result result = ybFeignClients.mesasn(mesasnDtos);
        //根据返回状态修改组托状态
        if (!result.getCode().equals(200)) {
            return ResultGenerator.fail(result.getMessage());
        } else {
            seedGroupBracketDto.setLaunchBy(BaseContextHandler.getUsername());
            seedGroupBracketDto.setLaunchDate(new Date());
            seedGroupBracketDto.setStatus(SeedStoreStatusEnum.待入库.getName());
            SeedGroupBracket seedGroupBracket = new SeedGroupBracket();
            BeanUtils.copyProperties(seedGroupBracketDto, seedGroupBracket);
            seedGroupBracketService.saveOrUpdate(seedGroupBracket);
            seedGroupBracketDto.getDetailsDtos().forEach(entity -> {
                SeedGroupBracketDetails seedGroupBracketDetails = new SeedGroupBracketDetails();
                BeanUtils.copyProperties(entity, seedGroupBracketDetails);
                seedGroupBracketDetails.setStatus(SeedStoreStatusEnum.待入库.getName());
                seedGroupBracketDetailsService.saveOrUpdate(seedGroupBracketDetails);
            });
        }
        return ResultGenerator.success();
    }

    @Override
    public Result storeInitiate(Long id) {
        SeedGroupBracketDto seedGroupBracketDto = seedGroupBracketService.findByIds(id);
        if(ObjectUtils.isEmpty(seedGroupBracketDto)){
            return ResultGenerator.fail("未查询到当前组托数据");
        }
        Result<ProductionOrderDto> listByAufnr = ybFeignClients.getListByAufnr(seedGroupBracketDto.getDetailsDtos().get(0).getPlanCode());
        if(listByAufnr.getCode().equals(200) && null != listByAufnr.getData()){
            seedGroupBracketDto.setWorkShopCode(listByAufnr.getData().getField4());
        }
        Result warehouseInputBill = seedGroupBracketService.createWarehouseInputBill(seedGroupBracketDto);
        if(!warehouseInputBill.getCode().equals(200)){
            return ResultGenerator.fail("发起入库失败");
        }
        SeedGroupBracket seedGroupBracket = new SeedGroupBracket();
        BeanUtils.copyProperties(seedGroupBracketDto,seedGroupBracket);
        seedGroupBracket.setStatus("待入库");
        seedGroupBracketService.saveOrUpdate(seedGroupBracket);
        seedGroupBracketDto.getDetailsDtos().stream().forEach(entity->{
            SeedGroupBracketDetails seedGroupBracketDetails = new SeedGroupBracketDetails();
            BeanUtils.copyProperties(entity,seedGroupBracketDetails);
            seedGroupBracketDetails.setStatus(seedGroupBracket.getStatus());
            seedGroupBracketDetailsService.saveOrUpdate(seedGroupBracketDetails);
        });
        return ResultGenerator.success("发起入库成功");
    }
}
