package com.ygtx.gxt.service.impl;

import java.util.Date;
import java.util.List;
import com.ygtx.common.utils.DateUtils;
import com.ygtx.common.utils.StringUtils;
import com.ygtx.gxt.domain.GxtRepairOrder;
import com.ygtx.gxt.domain.GxtWorkOrder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ygtx.gxt.mapper.GxtUserOperatorHistoryMapper;
import com.ygtx.gxt.domain.GxtUserOperatorHistory;
import com.ygtx.gxt.service.IGxtUserOperatorHistoryService;

import static com.ygtx.common.constant.UserConstants.OP_TYPE_LEADER;

/**
 * 用户操作历史记录Service业务层处理
 *
 * @author ruoyi
 * @date 2025-12-11
 */
@Service
public class GxtUserOperatorHistoryServiceImpl implements IGxtUserOperatorHistoryService
{
    @Autowired
    private GxtUserOperatorHistoryMapper gxtUserOperatorHistoryMapper;

    /**
     * 查询用户操作历史记录
     *
     * @param id 用户操作历史记录主键
     * @return 用户操作历史记录
     */
    @Override
    public GxtUserOperatorHistory selectGxtUserOperatorHistoryById(Long id)
    {
        return gxtUserOperatorHistoryMapper.selectGxtUserOperatorHistoryById(id);
    }

    /**
     * 查询用户操作历史记录列表
     *
     * @param gxtUserOperatorHistory 用户操作历史记录
     * @return 用户操作历史记录
     */
    @Override
    public List<GxtUserOperatorHistory> selectGxtUserOperatorHistoryList(GxtUserOperatorHistory gxtUserOperatorHistory)
    {
        return gxtUserOperatorHistoryMapper.selectGxtUserOperatorHistoryList(gxtUserOperatorHistory);
    }

    /**
     * 新增用户操作历史记录
     *
     * @param gxtUserOperatorHistory 用户操作历史记录
     * @return 结果
     */
    @Override
    public int insertGxtUserOperatorHistory(GxtUserOperatorHistory gxtUserOperatorHistory)
    {
        gxtUserOperatorHistory.setCreateTime(DateUtils.getNowDate());
        return gxtUserOperatorHistoryMapper.insertGxtUserOperatorHistory(gxtUserOperatorHistory);
    }

    /**
     * 修改用户操作历史记录
     *
     * @param gxtUserOperatorHistory 用户操作历史记录
     * @return 结果
     */
    @Override
    public int updateGxtUserOperatorHistory(GxtUserOperatorHistory gxtUserOperatorHistory)
    {
        gxtUserOperatorHistory.setUpdateTime(DateUtils.getNowDate());
        return gxtUserOperatorHistoryMapper.updateGxtUserOperatorHistory(gxtUserOperatorHistory);
    }

    /**
     * 批量删除用户操作历史记录
     *
     * @param ids 需要删除的用户操作历史记录主键
     * @return 结果
     */
    @Override
    public int deleteGxtUserOperatorHistoryByIds(Long[] ids)
    {
        return gxtUserOperatorHistoryMapper.deleteGxtUserOperatorHistoryByIds(ids);
    }

    /**
     * 删除用户操作历史记录信息
     *
     * @param id 用户操作历史记录主键
     * @return 结果
     */
    @Override
    public int deleteGxtUserOperatorHistoryById(Long id)
    {
        return gxtUserOperatorHistoryMapper.deleteGxtUserOperatorHistoryById(id);
    }

    public int insertHistoryRepairOrder(String fromType, GxtRepairOrder repairOrder){
        return insertOpHistory(fromType, repairOrder.getAcceptUserId(), repairOrder.getTeamLeaderId(), repairOrder.getTeamLeaderName(), repairOrder.getWorkPermitNum(), repairOrder.getUpdateBy());
    }

    public int insertHistoryWorkOrder(String fromType, GxtWorkOrder gxtWorkOrder){
        return insertOpHistory(fromType, gxtWorkOrder.getAcceptUserId(), gxtWorkOrder.getTeamLeaderId(), gxtWorkOrder.getTeamLeaderName(), gxtWorkOrder.getWorkPermitNum(), gxtWorkOrder.getUpdateBy());
    }

    private int insertOpHistory(String fromType, Long acceptUserId, Long teamLeaderId, String teamLeaderName, String workPermitNum, String updateBy) {
        try {
            GxtUserOperatorHistory gxtUserOperatorHistory = new GxtUserOperatorHistory();
            gxtUserOperatorHistory.setOpType(fromType);
            gxtUserOperatorHistory.setOpUserid(acceptUserId);
            if (OP_TYPE_LEADER.equals(fromType)) {
                if(teamLeaderId == null){
                    return 0;
                }
                gxtUserOperatorHistory.setOpLeaderId(teamLeaderId);
                gxtUserOperatorHistory.setOpLeaderName(teamLeaderName);
            } else {
                if(StringUtils.isEmpty(workPermitNum)){
                    return 0;
                }
                gxtUserOperatorHistory.setOpTicket(workPermitNum);
            }
            List<GxtUserOperatorHistory> list = selectGxtUserOperatorHistoryList(gxtUserOperatorHistory);
            if (list.isEmpty()) {
                gxtUserOperatorHistory.setOpNum(1L);
                gxtUserOperatorHistory.setCreateTime(new Date());
                gxtUserOperatorHistory.setCreateBy(updateBy);
                return insertGxtUserOperatorHistory(gxtUserOperatorHistory);
            } else {
                Long opNum = list.get(0).getOpNum() + 1L;
                gxtUserOperatorHistory.setId(list.get(0).getId());
                gxtUserOperatorHistory.setOpNum(opNum);
                gxtUserOperatorHistory.setUpdateTime(new Date());
                gxtUserOperatorHistory.setUpdateBy(updateBy);
                return updateGxtUserOperatorHistory(gxtUserOperatorHistory);
            }
        }catch (Exception ex){
            ex.printStackTrace();
        }
        return 0;
    }
}
