package com.ygtx.gxt.service.impl;

import java.util.List;
import java.util.Date;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ygtx.gxt.domain.GxtWorkOrderPerson;
import com.ygtx.gxt.mapper.GxtWorkOrderPersonMapper;
import com.ygtx.gxt.service.IGxtWorkOrderPersonService;
import com.ygtx.common.utils.SecurityUtils;

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

    /**
     * 查询工单工作组员
     *
     * @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;
    }
}
