package com.op.mes.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.op.common.core.utils.DateUtils;
import com.op.mes.domain.ProOrderWorkorder;
import com.op.mes.mapper.ProOrderMapper;
import com.op.mes.mapper.ProOrderWorkorderBatchMapper;
import com.op.mes.mapper.ProOrderWorkorderMapper;
import com.op.mes.service.IProOrderWorkorderService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;


/**
 * 生产工单Service业务层处理
 *
 * @author Open Platform
 * @date 2023-07-18
 */
@Service
public class ProOrderWorkorderServiceImpl implements IProOrderWorkorderService {
    @Autowired
    private ProOrderWorkorderMapper proOrderWorkorderMapper;
    @Autowired
    private ProOrderWorkorderBatchMapper proOrderWorkorderBatchMapper;
    @Autowired
    private ProOrderMapper proOrderMapper;
    /**
     * 查询生产工单
     *
     * @param workorderId 生产工单主键
     * @return 生产工单
     */
    @Override
    @DS("#header.poolName")
    public ProOrderWorkorder selectProOrderWorkorderByWorkorderId(String workorderId) {
        return proOrderWorkorderMapper.selectProOrderWorkorderByWorkorderId(workorderId);
    }

    /**
     * 查询生产工单列表
     *
     * @param proOrderWorkorder 生产工单
     * @return 生产工单
     */
    @Override
    @DS("#header.poolName")
    public List<ProOrderWorkorder> selectProOrderWorkorderList(ProOrderWorkorder proOrderWorkorder) {

        return proOrderWorkorderMapper.selectProOrderWorkorderList(proOrderWorkorder);
    }

    /**
     * 新增生产工单
     *
     * @param proOrderWorkorder 生产工单
     * @return 结果
     */
    @Override
    @DS("#header.poolName")
    public int insertProOrderWorkorder(ProOrderWorkorder proOrderWorkorder) {
        proOrderWorkorder.setCreateTime(DateUtils.getNowDate());
        return proOrderWorkorderMapper.insertProOrderWorkorder(proOrderWorkorder);
    }

    /**
     * 修改生产工单
     *
     * @param proOrderWorkorder 生产工单
     * @return 结果
     */
    @Override
    @DS("#header.poolName")
    public int updateProOrderWorkorder(ProOrderWorkorder proOrderWorkorder) {
        proOrderWorkorder.setUpdateTime(DateUtils.getNowDate());
        return proOrderWorkorderMapper.updateProOrderWorkorder(proOrderWorkorder);
    }

    /**
     * 批量删除生产工单
     *
     * @param workorderIds 需要删除的生产工单主键
     * @return 结果
     */
    @Override
    @DS("#header.poolName")
    @Transactional
    public int deleteProOrderWorkorderByWorkorderIds(String[] workorderIds) {
        for(String workorderId:workorderIds){
            //要删除的工单id们
            List<String> delteIds = new ArrayList<>();
            delteIds.add(workorderId);
            //最底级，用来还原订单拆分数量
            List<ProOrderWorkorder> workers = new ArrayList<>();
            ProOrderWorkorder work = proOrderWorkorderMapper.selectProOrderWorkorderByWorkorderId(workorderId);
            if(work != null){
                workers.add(work);
                ProOrderWorkorder bottomWork = work;
                Boolean run = true;
                while(run){
                    //上级
                    ProOrderWorkorder topworkers = proOrderWorkorderMapper.selectWorkOrderByCode(bottomWork.getParentOrder());
                    //有上级code
                    if(topworkers!=null) {
                        workers.add(topworkers);
                        delteIds.add(topworkers.getWorkorderId());
                        bottomWork.setParentOrder(topworkers.getParentOrder());
                    }else{
                        run = false;
                    }
                }
            }
            String[] workorderIds0 = (String[])delteIds.toArray(new String[delteIds.size()]);
            if(workorderIds0.length>0){
                //删除工单(字母)pro_order_workorder
                proOrderWorkorderMapper.deleteProOrderWorkorderByWorkorderIds(workorderIds0);
                //删除批次pro_order_workorder_batch
                proOrderWorkorderBatchMapper.deleteProOrderWorkorderBatchByWorkorderIds(workorderIds0);
            }

            if(!CollectionUtils.isEmpty(workers)){
                //释放订单拆分数pro_order
                proOrderMapper.updateOrderBatch(workers);
            }

        }
        return 1;
    }


    /**
     * 删除生产工单信息
     *
     * @param workorderId 生产工单主键
     * @return 结果
     */
    @Override
    @DS("#header.poolName")
    public int deleteProOrderWorkorderByWorkorderId(String workorderId) {
        return proOrderWorkorderMapper.deleteProOrderWorkorderByWorkorderId(workorderId);
    }

    /**
     * 下发生产工单信息
     * @param workorderIds 生产工单主键
     * @return 结果
     */
    @Override
    @DS("#header.poolName")
    public int downProOrderWorkorderByWorkorderIds(String[] workorderIds) {
        int m= proOrderWorkorderMapper.downProOrderWorkorderByWorkorderIds(workorderIds);
        return m;
    }
}
