package com.aucma.dms.service.impl;

import com.aucma.common.utils.DateUtils;
import com.aucma.common.utils.SecurityUtils;
import com.aucma.common.utils.uuid.Seq;
import com.aucma.dms.domain.DmsDispatchMember;
import com.aucma.dms.domain.DmsDispatchPlan;
import com.aucma.dms.mapper.DmsDispatchMemberMapper;
import com.aucma.dms.mapper.DmsDispatchPlanMapper;
import com.aucma.dms.service.IDmsDispatchPlanService;
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.Date;
import java.util.List;

/**
 * 派工计划Service业务层处理
 * 
 * @author Cascade AI
 * @date 2025-01-10
 */
@Service
public class DmsDispatchPlanServiceImpl implements IDmsDispatchPlanService {
    
    @Autowired
    private DmsDispatchPlanMapper dmsDispatchPlanMapper;
    
    @Autowired
    private DmsDispatchMemberMapper dmsDispatchMemberMapper;

    /**
     * 查询派工计划
     * 
     * @param planId 派工计划主键
     * @return 派工计划
     */
    @Override
    public DmsDispatchPlan selectDmsDispatchPlanByPlanId(Long planId) {
        DmsDispatchPlan plan = dmsDispatchPlanMapper.selectDmsDispatchPlanByPlanId(planId);
        if (plan != null && "2".equals(plan.getDispatchType())) {
            // 团队派工，查询成员列表
            DmsDispatchMember memberQuery = new DmsDispatchMember();
            memberQuery.setPlanId(planId);
            List<DmsDispatchMember> members = dmsDispatchMemberMapper.selectDmsDispatchMemberList(memberQuery);
            plan.setDispatchMembers(members);
        }
        return plan;
    }

    /**
     * 查询派工计划列表
     * 
     * @param dmsDispatchPlan 派工计划
     * @return 派工计划
     */
    @Override
    public List<DmsDispatchPlan> selectDmsDispatchPlanList(DmsDispatchPlan dmsDispatchPlan) {
        return dmsDispatchPlanMapper.selectDmsDispatchPlanList(dmsDispatchPlan);
    }

    /**
     * 新增派工计划
     * 
     * @param dmsDispatchPlan 派工计划
     * @return 结果
     */
    @Transactional
    @Override
    public int insertDmsDispatchPlan(DmsDispatchPlan dmsDispatchPlan) {
        // 生成派工单号
        if (dmsDispatchPlan.getPlanCode() == null || dmsDispatchPlan.getPlanCode().isEmpty()) {
            dmsDispatchPlan.setPlanCode(Seq.getId(Seq.dmsBillsFaultInstanceSeqType, "DP"));
        }
        
        // 设置默认值
        if (dmsDispatchPlan.getReceiveStatus() == null) {
            dmsDispatchPlan.setReceiveStatus("0"); // 待接收
        }
        if (dmsDispatchPlan.getExecutionStatus() == null) {
            dmsDispatchPlan.setExecutionStatus("0"); // 待执行
        }
        if (dmsDispatchPlan.getIsFlag() == null) {
            dmsDispatchPlan.setIsFlag("1");
        }
        if (dmsDispatchPlan.getDispatchTime() == null) {
            dmsDispatchPlan.setDispatchTime(new Date());
        }
        
        // 设置派工人信息
        dmsDispatchPlan.setDispatcherId(SecurityUtils.getUserId());
        dmsDispatchPlan.setDispatcherName(SecurityUtils.getUsername());
        dmsDispatchPlan.setCreateTime(DateUtils.getNowDate());
        dmsDispatchPlan.setCreateBy(SecurityUtils.getUserId());
        
        // 插入主表
        int rows = dmsDispatchPlanMapper.insertDmsDispatchPlan(dmsDispatchPlan);
        
        // 插入成员明细（团队派工）
        if ("2".equals(dmsDispatchPlan.getDispatchType())) {
            insertDispatchMembers(dmsDispatchPlan);
        }
        
        return rows;
    }

    /**
     * 修改派工计划
     * 
     * @param dmsDispatchPlan 派工计划
     * @return 结果
     */
    @Transactional
    @Override
    public int updateDmsDispatchPlan(DmsDispatchPlan dmsDispatchPlan) {
        dmsDispatchPlan.setUpdateTime(DateUtils.getNowDate());
        dmsDispatchPlan.setUpdateBy(SecurityUtils.getUserId());
        
        // 删除原有成员
        dmsDispatchMemberMapper.deleteDmsDispatchMemberByPlanId(dmsDispatchPlan.getPlanId());
        
        // 重新插入成员（团队派工）
        if ("2".equals(dmsDispatchPlan.getDispatchType())) {
            insertDispatchMembers(dmsDispatchPlan);
        }
        
        // 更新主表
        return dmsDispatchPlanMapper.updateDmsDispatchPlan(dmsDispatchPlan);
    }

    /**
     * 批量删除派工计划
     * 
     * @param planIds 需要删除的派工计划主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteDmsDispatchPlanByPlanIds(Long[] planIds) {
        // 删除成员数据
        for (Long planId : planIds) {
            dmsDispatchMemberMapper.deleteDmsDispatchMemberByPlanId(planId);
        }
        return dmsDispatchPlanMapper.deleteDmsDispatchPlanByPlanIds(planIds);
    }

    /**
     * 删除派工计划信息
     * 
     * @param planId 派工计划主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteDmsDispatchPlanByPlanId(Long planId) {
        // 删除成员数据
        dmsDispatchMemberMapper.deleteDmsDispatchMemberByPlanId(planId);
        return dmsDispatchPlanMapper.deleteDmsDispatchPlanByPlanId(planId);
    }

    /**
     * 接收派工
     * 
     * @param planId 派工计划ID
     * @return 结果
     */
    @Override
    public int receiveDispatch(Long planId) {
        DmsDispatchPlan updateEntity = new DmsDispatchPlan();
        updateEntity.setPlanId(planId);
        updateEntity.setReceiveStatus("1"); // 已接收
        updateEntity.setReceiveTime(new Date());
        updateEntity.setExecutionStatus("0"); // 待执行
        updateEntity.setUpdateTime(DateUtils.getNowDate());
        updateEntity.setUpdateBy(SecurityUtils.getUserId());
        
        return dmsDispatchPlanMapper.updateDmsDispatchPlan(updateEntity);
    }

    /**
     * 拒绝派工
     * 
     * @param dmsDispatchPlan 派工计划
     * @return 结果
     */
    @Override
    public int rejectDispatch(DmsDispatchPlan dmsDispatchPlan) {
        DmsDispatchPlan updateEntity = new DmsDispatchPlan();
        updateEntity.setPlanId(dmsDispatchPlan.getPlanId());
        updateEntity.setReceiveStatus("2"); // 已拒绝
        updateEntity.setReceiveTime(new Date());
        updateEntity.setRejectReason(dmsDispatchPlan.getRejectReason());
        updateEntity.setExecutionStatus("3"); // 已取消
        updateEntity.setUpdateTime(DateUtils.getNowDate());
        updateEntity.setUpdateBy(SecurityUtils.getUserId());
        
        return dmsDispatchPlanMapper.updateDmsDispatchPlan(updateEntity);
    }

    /**
     * 插入派工成员明细
     */
    private void insertDispatchMembers(DmsDispatchPlan plan) {
        List<DmsDispatchMember> members = plan.getDispatchMembers();
        if (!CollectionUtils.isEmpty(members)) {
            for (DmsDispatchMember member : members) {
                member.setPlanId(plan.getPlanId());
                member.setCreateTime(DateUtils.getNowDate());
                member.setCreateBy(SecurityUtils.getUserId());
                dmsDispatchMemberMapper.insertDmsDispatchMember(member);
            }
        }
    }
}
