package com.ygtx.gxt.service.impl;

import java.util.List;
import java.util.Date;

import com.ygtx.common.exception.ServiceException;
import com.ygtx.common.utils.SecurityUtils;
import com.ygtx.gxt.domain.GxtWorkOrder;
import com.ygtx.gxt.mapper.GxtWorkOrderMapper;
import com.ygtx.gxt.mapper.GxtWorkOrderPersonMapper;
import com.ygtx.gxt.service.IGxtWorkOrderPersonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ygtx.gxt.domain.GxtWorkOrderPerson;
import com.ygtx.common.exception.ServiceException;
import com.ygtx.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;

/**
 * 工单工作组员Service业务层处理
 *
 * @author ygtx
 * @date 2025-10-29
 */
@Service
public class GxtWorkOrderPersonServiceImpl implements IGxtWorkOrderPersonService {
    @Autowired
    private GxtWorkOrderPersonMapper gxtWorkOrderPersonMapper;

    @Autowired
    private GxtWorkOrderMapper gxtWorkOrderMapper;

    /**
     * 查询工单工作组员
     *
     * @param id 工单工作组员主键
     * @return 工单工作组员
     */
    @Override
    public GxtWorkOrderPerson selectGxtWorkOrderPersonById(Long id) {
        return gxtWorkOrderPersonMapper.selectGxtWorkOrderPersonById(id);
    }

    /**
     * 查询工单工作组员列表
     *
     * @param gxtWorkOrderPerson 工单工作组员
     * @return 工单工作组员
     */
    @Override
    public List<GxtWorkOrderPerson> selectGxtWorkOrderPersonList(GxtWorkOrderPerson gxtWorkOrderPerson) {
        return gxtWorkOrderPersonMapper.selectGxtWorkOrderPersonList(gxtWorkOrderPerson);
    }

    /**
     * 新增工单工作组员
     *
     * @param gxtWorkOrderPerson 工单工作组员
     * @return 结果
     */
    @Override
    public int insertGxtWorkOrderPerson(GxtWorkOrderPerson gxtWorkOrderPerson) {
        gxtWorkOrderPerson.setCreateTime(new Date());
        gxtWorkOrderPerson.setCreateBy(SecurityUtils.getUsername());
        return gxtWorkOrderPersonMapper.insertGxtWorkOrderPerson(gxtWorkOrderPerson);
    }

    /**
     * 修改工单工作组员
     *
     * @param gxtWorkOrderPerson 工单工作组员
     * @return 结果
     */
    @Override
    public int updateGxtWorkOrderPerson(GxtWorkOrderPerson gxtWorkOrderPerson) {
        gxtWorkOrderPerson.setUpdateTime(new Date());
        gxtWorkOrderPerson.setUpdateBy(SecurityUtils.getUsername());
        return gxtWorkOrderPersonMapper.updateGxtWorkOrderPerson(gxtWorkOrderPerson);
    }

    /**
     * 删除工单工作组员
     *
     * @param id 工单工作组员主键
     * @return 结果
     */
    @Override
    public int deleteGxtWorkOrderPersonById(Long id) {
        return gxtWorkOrderPersonMapper.deleteGxtWorkOrderPersonById(id);
    }

    /**
     * 批量删除工单工作组员
     *
     * @param ids 需要删除的数据主键集合
     * @return 结果
     */
    @Override
    public int deleteGxtWorkOrderPersonByIds(Long[] ids) {
        return gxtWorkOrderPersonMapper.deleteGxtWorkOrderPersonByIds(ids);
    }

    /**
     * 根据工单ID查询工作组员列表
     *
     * @param orderId 工单ID
     * @return 工作组员集合
     */
    @Override
    public List<GxtWorkOrderPerson> selectGxtWorkOrderPersonByOrderId(Long orderId) {
        return gxtWorkOrderPersonMapper.selectGxtWorkOrderPersonByOrderId(orderId);
    }

    /**
     * 根据工单ID删除工作组员
     *
     * @param orderId 工单ID
     * @return 结果
     */
    @Override
    public int deleteGxtWorkOrderPersonByOrderId(Long orderId) {
        return gxtWorkOrderPersonMapper.deleteGxtWorkOrderPersonByOrderId(orderId);
    }

    /**
     * 批量添加工作组员
     *
     * @param orderId    工单ID
     * @param orderCode  工单编码
     * @param personList 工作组员列表
     * @return 结果
     */
    @Override
    public int insertBatchGxtWorkOrderPerson(Long orderId, String orderCode, List<GxtWorkOrderPerson> personList) {
        if (personList == null || personList.isEmpty()) {
            return 0;
        }

        int count = 0;
        for (GxtWorkOrderPerson person : personList) {
            person.setOrderId(orderId);
            person.setOrderCode(orderCode);
            count += insertGxtWorkOrderPerson(person);
        }
        return count;
    }

    /**
     * 从文件中导入工单人员
     *
     * @param personList Excel转换的人员列表
     * @return 结果
     */
    @Override
    @Transactional
    public String importGxtWorkOrderPerson(List<GxtWorkOrderPerson> personList) {
        if (StringUtils.isNull(personList) || personList.size() == 0) {
            throw new ServiceException("导入工单人员数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        int row = 1;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();

        for (GxtWorkOrderPerson person : personList) {
            try {
                row++;
                if (StringUtils.isEmpty(person.getMisNo())) {
                    throw new ServiceException("第" + row + "行MIS系统编号不能为空！");
                }

                // 根据MIS系统编号查询工单
                GxtWorkOrder workOrder = gxtWorkOrderMapper.selectGxtWorkOrderByMisNo(person.getMisNo());
                if (workOrder == null) {
                    throw new ServiceException("第" + row + "行工单不存在或MIS编号错误！");
                }

                // 为该人员设置工单信息
                person.setOrderId(workOrder.getId());
                person.setOrderCode(workOrder.getWorkOrderProjectNo());

                // 插入人员记录
                insertGxtWorkOrderPerson(person);
                successNum++;
            } catch (Exception e) {
                failureNum++;
                String msg = "<br/>" + failureNum + "、人员导入失败: ";
                failureMsg.append(msg + e.getMessage());
            }
        }
        if (failureNum > 0) {
            failureMsg.insert(0, "导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        } else {
            successMsg.insert(0, "数据已全部导入成功！共 " + successNum + " 条");
        }
        return successMsg.toString();
    }
}
