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


import com.sw.mes.core.api.system.StoreFeignClients;
import com.sw.mes.core.base.BaseServiceImpl;
import com.sw.mes.core.dto.store.LockInventoryDto;
import com.sw.mes.core.response.Result;
import com.sw.mes.core.response.ResultCode;
import com.sw.mes.utils.StringUtils;
import com.sw.mes.yb.production.dto.AgainTask.AgainTaskDto;
import com.sw.mes.yb.production.dto.AgainTaskDetail.AgainTaskDetailDto;
import com.sw.mes.yb.production.dto.AgainTaskPrepare.InputTaskDatailPrepareDto;
import com.sw.mes.yb.production.dto.InputFurnaceBurden.InputFurnaceBurdenDto;
import com.sw.mes.yb.production.dto.InputFurnaceDetailDto;
import com.sw.mes.yb.production.entity.AgainTask;
import com.sw.mes.yb.production.entity.AgainTaskDetail;
import com.sw.mes.yb.production.entity.AgainTaskPrepare.AgainTaskPrepare;
import com.sw.mes.yb.production.entity.AgainTaskPrepare.InputTaskDatailPrepare;
import com.sw.mes.yb.production.entity.InputFurnaceBurden;
import com.sw.mes.yb.production.entity.InputFurnaceDetail;
import org.modelmapper.ModelMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.sw.mes.yb.production.dao.InputTaskDatailPrepareDao;
import com.sw.mes.yb.production.service.InputTaskDatailPrepareService;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;


/**
* @Author
* @Description：
* @Date: Create in
* @Modified By:
*/
@Service
@Transactional
public class InputTaskDatailPrepareServiceImpl extends BaseServiceImpl<InputTaskDatailPrepare> implements InputTaskDatailPrepareService {
    InputTaskDatailPrepareDao dao;
	@Autowired
    public void setDao(InputTaskDatailPrepareDao dao) {
        super.setBaseDao(dao);
        this.dao=dao;
    }

    @Autowired
    ModelMapper modelMapper;
    @Autowired
    private StoreFeignClients storeFeignClients;

    @Override
    public void endInput(AgainTaskPrepare task, List<InputTaskDatailPrepareDto> list) {
        List<LockInventoryDto> lockInventoryList = new ArrayList<>();	// 锁定库存明细
        // 保存投料明细
        List<String> bucketBatchNos = new ArrayList<>();
        for (InputTaskDatailPrepareDto inputFurnaceDetailDto : list) {
            if(inputFurnaceDetailDto.getWeight() == null
                    || inputFurnaceDetailDto.getWeight().compareTo(BigDecimal.ZERO) <= 0 ){
                throw new RuntimeException("重量不允许为0或者负数");
            }

            // 单桶内物料不能重复
            String bucketBatchNo = inputFurnaceDetailDto.getBucketNo() +"-"+ inputFurnaceDetailDto.getBatchNo();
            if(bucketBatchNos.contains(bucketBatchNo)){
                throw new RuntimeException("物料有重复，请检查！");
            }

            InputTaskDatailPrepare detail = fromDto(inputFurnaceDetailDto);
            detail.setProductionAgainTaskPrepareId(task.getId());
            this.saveOrUpdate(detail);

            // 物料唯一码
            bucketBatchNos.add(bucketBatchNo);

            // 组织锁定库存数据
            LockInventoryDto inventory = new LockInventoryDto();
            inventory.setMaterialModelCode(detail.getMaterialCode());
            inventory.setBatchNo(detail.getBatchNo());
            inventory.setUniqueCode(detail.getUniqueCode()==null? "": detail.getUniqueCode());
            inventory.setWarehouseId(detail.getStoreLocationId().longValue());
            inventory.setQuantity(detail.getWeight());
            lockInventoryList.add(inventory);
        }

        // 锁定库存
        Result result = storeFeignClients.lockInventory(lockInventoryList);
        if (result.getCode() != ResultCode.SUCCESS.getCode()){
            throw new RuntimeException("线边仓锁定库存失败：" + result.getMessage());
        }
    }

    @Override
    public List<InputTaskDatailPrepare> findByProductionAgainTaskPrepareId(Long productionAgainTaskPrepareId) {
        return dao.findByProductionAgainTaskPrepareId(productionAgainTaskPrepareId);
    }

    public InputTaskDatailPrepare fromDto(InputTaskDatailPrepareDto dto) {
        return modelMapper.map(dto, InputTaskDatailPrepare.class);
    }

}
