package com.op.mes.service.impl;

import java.util.*;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.op.common.core.context.SecurityContextHolder;
import com.op.common.core.utils.DateUtils;
import com.op.common.core.utils.uuid.IdUtils;
import com.op.common.core.utils.uuid.UUID;
import com.op.common.core.web.domain.AjaxResult;
import com.op.mes.domain.*;
import com.op.mes.mapper.ProOrderMapper;
import com.op.mes.mapper.ProOrderWorkorderMapper;
import com.op.mes.mapper.ProWetMaterialPlanDetailMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.op.mes.mapper.ProWetMaterialPlanMapper;
import com.op.mes.service.IProWetMaterialPlanService;
import org.springframework.transaction.annotation.Transactional;

import static com.op.common.core.web.domain.AjaxResult.error;
import static com.op.common.core.web.domain.AjaxResult.success;

/**
 * 湿料计划管理Service业务层处理
 *
 * @author Open Platform
 * @date 2023-07-24
 */
@Service
public class ProWetMaterialPlanServiceImpl implements IProWetMaterialPlanService {
    @Autowired
    private ProWetMaterialPlanMapper proWetMaterialPlanMapper;
    @Autowired
    private ProOrderWorkorderMapper proOrderWorkorderMapper;
    @Autowired
    private ProOrderMapper proOrderMapper;
    @Autowired
    private ProWetMaterialPlanDetailMapper proWetMaterialPlanDetailMapper;

    /**
     * 查询湿料计划管理
     *
     * @param id 湿料计划管理主键
     * @return 湿料计划管理
     */
    @Override
    @DS("#header.poolName")
    public ProWetMaterialPlan selectProWetMaterialPlanById(String id) {
        // 参数判空
        if (id == null || id.equals("")) {
            return null;
        }
        return proWetMaterialPlanMapper.selectProWetMaterialPlanById(id);
    }

    /**
     * 查询湿料计划管理列表
     *
     * @param proWetMaterialPlan 湿料计划管理
     * @return 湿料计划管理
     */
    @Override
    @DS("#header.poolName")
    public List<ProWetMaterialPlan> selectProWetMaterialPlanList(ProWetMaterialPlan proWetMaterialPlan) {
        if (proWetMaterialPlan.getStartTime() != null && proWetMaterialPlan.getEndTime() != null) {
            return proWetMaterialPlanMapper.selectProWetMaterialPlanListByTime(proWetMaterialPlan.getStartTime(), proWetMaterialPlan.getEndTime());
        }
        return proWetMaterialPlanMapper.selectProWetMaterialPlanList(proWetMaterialPlan);
    }

    /**
     * 新增湿料计划管理
     *
     * @param proWetMaterialPlan 湿料计划管理
     * @return 结果
     */
    @Override
    @DS("#header.poolName")
    public int insertProWetMaterialPlan(ProWetMaterialPlan proWetMaterialPlan) {
        proWetMaterialPlan.setCreateTime(DateUtils.getNowDate());
        return proWetMaterialPlanMapper.insertProWetMaterialPlan(proWetMaterialPlan);
    }

    /**
     * 修改湿料计划管理
     *
     * @param proWetMaterialPlan 湿料计划管理
     * @return 结果
     */
    @Override
    @DS("#header.poolName")
    public int updateProWetMaterialPlan(ProWetMaterialPlan proWetMaterialPlan) {
        proWetMaterialPlan.setUpdateTime(DateUtils.getNowDate());
        return proWetMaterialPlanMapper.updateProWetMaterialPlan(proWetMaterialPlan);
    }

    /**
     * 批量删除湿料计划管理
     *
     * @param ids 需要删除的湿料计划管理主键
     * @return 结果
     */
    @Override
    @DS("#header.poolName")
    public int deleteProWetMaterialPlanByIds(String[] ids) {
        return proWetMaterialPlanMapper.deleteProWetMaterialPlanByIds(ids);
    }

    /**
     * 删除湿料计划管理信息
     *
     * @param id 湿料计划管理主键
     * @return 结果
     */
    @Override
    @DS("#header.poolName")
    public int deleteProWetMaterialPlanById(String id) {
        return proWetMaterialPlanMapper.deleteProWetMaterialPlanById(id);
    }

    /**
     * 通过工单生产日期查询所有子工单
     *
     * @param productDate
     * @return
     */
    @Override
    @DS("#header.poolName")
    public AjaxResult getWorkOrderByTime(Date productDate) {
        // 参数判空
        if (productDate == null) {
            return error("参数为空！");
        }
        List<ProOrderWorkorder> workorderList = proOrderWorkorderMapper.selectWorkOrderByTime(productDate);
        // 循环遍历
        for (int i = 0; i < workorderList.size(); i++) {
            // 通过班次id获取班次名称
            workorderList.get(i).setShiftDesc(proWetMaterialPlanMapper.selectShiftById(workorderList.get(i).getShiftId()));
        }
        return success(workorderList);
    }

    /**
     * 查询料罐list
     *
     * @return
     */
    @Override
    @DS("#header.poolName")
    public AjaxResult getBucketList() {
        return success(proWetMaterialPlanMapper.selectBucketList());
    }

    /**
     * 获取物料list
     *
     * @return
     */
    @Override
    @DS("#header.poolName")
    public AjaxResult getProductList() {
        return success(proWetMaterialPlanMapper.selectProductList());
    }

    /**
     * 通过计划日期获取当前已经分配的料罐、物料、班次信息list
     *
     * @param planTime
     * @return
     */
    @Override
    @DS("#header.poolName")
    public AjaxResult getBMSList(Date planTime) {
        // 参数判空
        if (planTime == null) {
            return null;
        }
        return success(proWetMaterialPlanMapper.selectBMSList(planTime));
    }

    /**
     * 提交湿料计划接口
     *
     * @param proOrderWorkorderList
     * @return
     */
    @Override
    @DS("#header.poolName")
    @Transactional
    public AjaxResult addWetPlan(List<ProOrderWorkorder> proOrderWorkorderList) {
        // 获取料罐信息list
        List<Bucket> bucketList = proWetMaterialPlanMapper.selectBucketList();
        // 获取班次信息list
        List<ProShift> shiftList = proOrderMapper.selectProShift();

        // 湿料计划分类
        // 在该班次下
        for (int shift = 0; shift < shiftList.size(); shift++){
            // 在该料罐下
            for (int bucket = 0; bucket < bucketList.size(); bucket++){
                int onlyOne = 0;
                // 循环传入的工单信息
                for (int order = 0; order < proOrderWorkorderList.size(); order++){
                    // 如果该工单在该班次并且在该料罐下
                    if (proOrderWorkorderList.get(order).getShiftId().equals(shiftList.get(shift).getShiftId().toString()) && (proOrderWorkorderList.get(order).getBucketId() == bucketList.get(bucket).getBucketId())){
                        // 同班次同料罐下所有工单进行相同操作
                        // 如果首次创建主计划
                        if (onlyOne==0){
                            // 创建主湿料计划
                            ProWetMaterialPlan proWetMaterialPlan = new ProWetMaterialPlan();
                            // 设置主键id
                            proWetMaterialPlan.setId(IdUtils.fastSimpleUUID());
                            // TODO 计划工厂编码
                            // 订单类型
                            proWetMaterialPlan.setProdType(proOrderWorkorderList.get(order).getProdType());
                            // 物料号
                            proWetMaterialPlan.setProdCode(proOrderWorkorderList.get(order).getProductCode());
                            // 物料名称
                            proWetMaterialPlan.setProdDesc(proOrderWorkorderList.get(order).getProductName());
                            // 存放大罐Code
                            proWetMaterialPlan.setProdSource(proOrderWorkorderList.get(order).getBucketCode());
                            // 计划状态（1正常0停用）
                            proWetMaterialPlan.setStatus("1");
                            // 创建者
                            proWetMaterialPlan.setCreateBy(SecurityContextHolder.getUserName());
                            // 创建时间
                            proWetMaterialPlan.setCreateTime(new Date());
                            // 更新者
                            proWetMaterialPlan.setUpdateBy(SecurityContextHolder.getUserName());
                            // 更新时间
                            proWetMaterialPlan.setUpdateTime(new Date());
                            // 同步状态（1已同步0未同步）
                            proWetMaterialPlan.setSyncFlag("0");
                            // 计划编码
                            proWetMaterialPlan.setProdCode("SL"+ DateUtils.parseDateToStr(DateUtils.YYYYMMDDHHMMSSsss, new Date()));
                            // 计划日期
                            proWetMaterialPlan.setPlanTime(proOrderWorkorderList.get(order).getPlanTime());
                            // 班次id
                            proWetMaterialPlan.setShiftId(Integer.valueOf(proOrderWorkorderList.get(order).getShiftId()));
                            // 班次名称
                            proWetMaterialPlan.setShiftDesc(proOrderWorkorderList.get(order).getShiftDesc());
                            // 将主湿料计划加入到数据库
                            proWetMaterialPlanMapper.insertProWetMaterialPlan(proWetMaterialPlan);

                            // 创建湿料计划详情
                            ProWetMaterialPlanDetail proWetMaterialPlanDetail = new ProWetMaterialPlanDetail();
                            // 主键id
                            proWetMaterialPlanDetail.setId(IdUtils.fastSimpleUUID());
                            // TODO 计划工厂编码
                            // 配料计划id
                            proWetMaterialPlanDetail.setWetMaterialPlanId(proWetMaterialPlan.getId());
                            // 工单id
                            proWetMaterialPlanDetail.setWorkorderId(proOrderWorkorderList.get(order).getWorkorderId());
                            // 接料状态（1接料完成0未接料）
                            proWetMaterialPlanDetail.setStatus("0");
                            // 料罐id
                            proWetMaterialPlanDetail.setBucketId(String.valueOf(proOrderWorkorderList.get(order).getBucketId()));
                            // 料罐编码
                            proWetMaterialPlanDetail.setBucketCode(proOrderWorkorderList.get(order).getBucketCode());
                            // 物料编码
                            proWetMaterialPlanDetail.setMaterialCode(proOrderWorkorderList.get(order).getMaterialCode());
                            // 物料id
                            proWetMaterialPlanDetail.setMaterialId(String.valueOf(proOrderWorkorderList.get(order).getMaterialId()));
                            // 物料名称
                            proWetMaterialPlanDetail.setMaterialName(proOrderWorkorderList.get(order).getMaterialName());
                            // 产品名称
                            proWetMaterialPlanDetail.setProductName(proOrderWorkorderList.get(order).getProductName());
                            // 班次id
                            proWetMaterialPlanDetail.setShiftId(Long.valueOf(proOrderWorkorderList.get(order).getShiftId()));
                            // TODO 班次编码
                            // 班次简称
                            proWetMaterialPlanDetail.setShiftDesc(proOrderWorkorderList.get(order).getShiftDesc());

                            proWetMaterialPlanDetailMapper.insertProWetMaterialPlanDetail(proWetMaterialPlanDetail);

                        }else {
                            // 查询当天下-该班次-该料罐下的主湿料计划的id
                            String id = proWetMaterialPlanMapper.selectProWetMaterialPlanByPlanTime(proOrderWorkorderList.get(order).getPlanTime(),proOrderWorkorderList.get(order).getBucketCode(),proOrderWorkorderList.get(order).getShiftId());

                            // 创建湿料计划详情
                            ProWetMaterialPlanDetail proWetMaterialPlanDetail = new ProWetMaterialPlanDetail();
                            // 主键id
                            proWetMaterialPlanDetail.setId(IdUtils.fastSimpleUUID());
                            // TODO 计划工厂编码
                            // 配料计划id
                            proWetMaterialPlanDetail.setWetMaterialPlanId(id);
                            // 工单id
                            proWetMaterialPlanDetail.setWorkorderId(proOrderWorkorderList.get(order).getWorkorderId());
                            // 接料状态（1接料完成0未接料）
                            proWetMaterialPlanDetail.setStatus("0");
                            // 料罐id
                            proWetMaterialPlanDetail.setBucketId(String.valueOf(proOrderWorkorderList.get(order).getBucketId()));
                            // 料罐编码
                            proWetMaterialPlanDetail.setBucketCode(proOrderWorkorderList.get(order).getBucketCode());
                            // 物料编码
                            proWetMaterialPlanDetail.setMaterialCode(proOrderWorkorderList.get(order).getMaterialCode());
                            // 物料id
                            proWetMaterialPlanDetail.setMaterialId(String.valueOf(proOrderWorkorderList.get(order).getMaterialId()));
                            // 物料名称
                            proWetMaterialPlanDetail.setMaterialName(proOrderWorkorderList.get(order).getMaterialName());
                            // 产品名称
                            proWetMaterialPlanDetail.setProductName(proOrderWorkorderList.get(order).getProductName());
                            // 班次id
                            proWetMaterialPlanDetail.setShiftId(Long.valueOf(proOrderWorkorderList.get(order).getShiftId()));
                            // TODO 班次编码
                            // 班次简称
                            proWetMaterialPlanDetail.setShiftDesc(proOrderWorkorderList.get(order).getShiftDesc());

                            proWetMaterialPlanDetailMapper.insertProWetMaterialPlanDetail(proWetMaterialPlanDetail);
                        }
                        onlyOne+=1;
                    }
                }
            }
        }

        return null;
    }

}
