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

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sw.mes.core.context.BaseContextHandler;
import com.sw.mes.core.enums.EnumValuesFactory;
import com.sw.mes.core.response.Result;
import com.sw.mes.core.response.ResultGenerator;
import com.sw.mes.itemclean.clean.domain.CleaningProcessFlow;
import com.sw.mes.itemclean.clean.dto.MaterialBrokenUpAndDownDto;
import com.sw.mes.itemclean.clean.dto.WorkInCleanProcessParamQuery;
import com.sw.mes.itemclean.clean.dto.WorkInCleanProgressDetailDto;
import com.sw.mes.itemclean.clean.dto.WorkInCleanProgressDto;
import com.sw.mes.itemclean.clean.mapper.CleaningProcessFlowMapper;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.sw.mes.itemclean.clean.service.CleaningProcessFlowService;

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


/**
 * @Author
 * @Description：
 * @Date: Create in
 * @Modified By:
 */
@Service
@Transactional
public class CleaningProcessFlowServiceImpl extends ServiceImpl<CleaningProcessFlowMapper, CleaningProcessFlow>
        implements CleaningProcessFlowService {

    @Autowired
    CleaningProcessFlowMapper dao;

    @Override
    public Result inStation(String workOrder, String masterWorkOrder, BigDecimal weight, BigDecimal unusedWeight, BigDecimal usedWeight, String deviceNo, String wipStorageCode, String flowLineLable, String boxNo, String lastSite, Integer type) {

        CleaningProcessFlow cleaningProcessFlow = initData(workOrder, masterWorkOrder, weight, unusedWeight, usedWeight, deviceNo, wipStorageCode, type, boxNo, lastSite);
        return ResultGenerator.success(cleaningProcessFlow);
    }

    @Override
    public Result outStation(String workOrder, String masterWorkOrder, BigDecimal weight, BigDecimal unusedWeight, BigDecimal usedWeight, String deviceNo, String wipStorageCode, String flowLineLable, String boxNo, String lastSite, Integer type) {

        CleaningProcessFlow cleaningProcessFlow = initData(workOrder, masterWorkOrder, weight, unusedWeight, usedWeight, deviceNo, wipStorageCode, type, boxNo, lastSite);
        return ResultGenerator.success(cleaningProcessFlow);
    }

    @Override
    public CleaningProcessFlow getByBoxNo(String boxNo) {
        return dao.getByBoxNo(boxNo);
    }

    @Override
    public IPage<WorkInCleanProgressDto> queryWorkInProcessPage(IPage<WorkInCleanProgressDto> iPage, WorkInCleanProcessParamQuery query) {
        return baseMapper.queryWorkInProcessPage(iPage, query);
    }

    @Override
    public List<WorkInCleanProgressDto> queryWorkInProcessList(WorkInCleanProcessParamQuery query) {
        return baseMapper.queryWorkInProcessList(query);
    }

    @Override
    public List<WorkInCleanProgressDetailDto> findDetailByMaterialCode(WorkInCleanProcessParamQuery query) {
        return baseMapper.findDetailByMaterialCode(query);
    }

    public CleaningProcessFlow initData(String workOrder, String masterWorkOrder, BigDecimal weight, BigDecimal unusedWeight, BigDecimal usedWeight, String deviceNo, String wipStorageCode, Integer type, String boxNo, String lastSite) {
        CleaningProcessFlow cleaningProcessFlow = new CleaningProcessFlow();
        if(type.equals(EnumValuesFactory.CleaningWipStorageStatus.工序进站.getKey())){
            cleaningProcessFlow.setSiteName(wipStorageCode + EnumValuesFactory.CleaningWipStorageStatus.工序进站.getValue());
            cleaningProcessFlow.setCurrentSiteName(wipStorageCode);
            cleaningProcessFlow.setStatus(1);
        } else if(type.equals(EnumValuesFactory.CleaningWipStorageStatus.工序待进站.getKey())){
            cleaningProcessFlow.setSiteName("进入" + wipStorageCode + EnumValuesFactory.CleaningWipStorageStatus.工序待进站.getValue());
            cleaningProcessFlow.setCurrentSiteName(wipStorageCode);
            cleaningProcessFlow.setStatus(0);
        } else {
            cleaningProcessFlow.setSiteName(wipStorageCode + EnumValuesFactory.CleaningWipStorageStatus.工序出站.getValue());
            cleaningProcessFlow.setCurrentSiteName(wipStorageCode);
            cleaningProcessFlow.setStatus(2);
        }
        cleaningProcessFlow.setCreateTime(new Date());
        cleaningProcessFlow.setCreateUser(BaseContextHandler.getUsername());
        cleaningProcessFlow.setDeviceNo(deviceNo);
        cleaningProcessFlow.setMasterWorkOrder(masterWorkOrder);
        cleaningProcessFlow.setUnusedWeight(unusedWeight);
        cleaningProcessFlow.setUsedWeight(usedWeight);
        cleaningProcessFlow.setWeight(weight);
        cleaningProcessFlow.setWorkOrder(workOrder);
        cleaningProcessFlow.setBoxNo(boxNo);
        cleaningProcessFlow.setLastSite(lastSite);
        this.saveOrUpdate(cleaningProcessFlow);
        return cleaningProcessFlow;
    }
}
