package com.foreverwin.mesnac.production.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.foreverwin.mesnac.common.enums.HandleEnum;
import com.foreverwin.mesnac.common.service.CommonService;
import com.foreverwin.mesnac.common.util.StringUtil;
import com.foreverwin.mesnac.dispatch.model.ItemBatch;
import com.foreverwin.mesnac.dispatch.service.ItemBatchService;
import com.foreverwin.mesnac.meapi.model.Item;
import com.foreverwin.mesnac.meapi.model.Operation;
import com.foreverwin.mesnac.meapi.service.ItemService;
import com.foreverwin.mesnac.meapi.service.SfcService;
import com.foreverwin.mesnac.production.mapper.AccessoryLogMapper;
import com.foreverwin.mesnac.production.model.AccessoryLog;
import com.foreverwin.mesnac.production.service.AccessoryLogService;
import com.foreverwin.modular.core.exception.BaseException;
import com.foreverwin.modular.core.util.CommonMethods;
import com.foreverwin.modular.core.util.FrontPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 * 辅料使用记录表 服务实现类
 * </p>
 *
 * @author Philip
 * @since 2021-08-12
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class AccessoryLogServiceImpl extends ServiceImpl<AccessoryLogMapper, AccessoryLog> implements AccessoryLogService {


    @Autowired
    private AccessoryLogMapper accessoryLogMapper;
    @Autowired
    private ItemBatchService itemBatchService;
    @Autowired
    private CommonService commonService;
    @Autowired
    private ItemService itemService;
    @Autowired
    private SfcService sfcService;
    @Override
    public IPage<AccessoryLog> selectPage(FrontPage<AccessoryLog> frontPage, AccessoryLog accessoryLog) {
        QueryWrapper<AccessoryLog> queryWrapper = new QueryWrapper<>();
        queryWrapper.setEntity(accessoryLog);
        return super.page(frontPage.getPagePlus(), queryWrapper);
    }

    @Override
    public List<AccessoryLog> selectList(AccessoryLog accessoryLog) {
        QueryWrapper<AccessoryLog> queryWrapper = new QueryWrapper<>();
        queryWrapper.setEntity(accessoryLog);
        return super.list(queryWrapper);
    }

    @Override
    public void loadConfirm(AccessoryLog accessoryLog) {
        String site = CommonMethods.getSite();
        String resrce = accessoryLog.getResrce();
        String sfc = accessoryLog.getSfc();
        BigDecimal qty = accessoryLog.getQty();
        String label = accessoryLog.getLabel();
        QueryWrapper<ItemBatch> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq(ItemBatch.SITE,site);
        queryWrapper.eq(ItemBatch.LABEL,label);
        List<ItemBatch> list = itemBatchService.list(queryWrapper);
        if (qty.compareTo(BigDecimal.ZERO)<=0){
            throw new BaseException("使用数量不能小于零");
        }
        if (list.isEmpty()){
            throw new BaseException("标签不存在");
        }
        ItemBatch itemBatch = list.get(0);
        String workShopBo = commonService.getWorkShopBo(HandleEnum.RESOURCE.getHandle(site,resrce));
        if (StringUtil.isBlank(workShopBo)) {
            throw new BaseException("资源 " + resrce + " 未匹配到车间");
        }
        String sfcBO = HandleEnum.SFC.getHandle(site, sfc);
        Operation operationBySfcBo = commonService.getOperationBySfcBo(sfcBO);
        if (operationBySfcBo==null){
            throw new BaseException("未找到产品条码信息");
        }
        accessoryLog.setHandle(UUID.randomUUID().toString());
        accessoryLog.setSite(site);
        accessoryLog.setWorkCenter(StringUtil.trimHandle(workShopBo));
        accessoryLog.setShopOrder(StringUtil.trimHandle(sfcService.getById(sfcBO).getShopOrderBo()));
        accessoryLog.setOperation(operationBySfcBo.getOperation());
        accessoryLog.setStepId(operationBySfcBo.getStepId());
        accessoryLog.setBatch(itemBatch.getBatch());
        String item = itemBatch.getItem();
        Item selectCurrent = itemService.selectCurrent(site, item);
        if (selectCurrent==null){
            throw new BaseException("标签物料不存在当前版本");
        }
        accessoryLog.setItem(item);
        accessoryLog.setRevision(selectCurrent.getRevision());
        accessoryLog.setCreateUser(CommonMethods.getUser());
        accessoryLog.setCreatedDateTime(LocalDateTime.now());
        save(accessoryLog);
    }

    @Override
    public List<AccessoryLog> getLoadLabelList(AccessoryLog accessoryLog) {
        String site = CommonMethods.getSite();
        String sfc = accessoryLog.getSfc();
        String sfcBO = HandleEnum.SFC.getHandle(site, sfc);
        Operation operationBySfcBo = commonService.getOperationBySfcBo(sfcBO);
        if (operationBySfcBo==null){
            throw new BaseException("未找到产品条码信息");
        }
        if (operationBySfcBo==null){
            throw new BaseException("未找到产品条码信息");
        }
        return accessoryLogMapper.getLabelList(site,sfc,operationBySfcBo.getStepId());
    }


}