package com.hw.system.common.service.impl;

import java.util.List;
import com.hw.common.core.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.hw.system.common.mapper.WfProcessMapper;
import com.hw.system.common.domain.WfProcess;
import com.hw.system.common.service.IWfProcessService;

/**
 * 工单流程Service业务层处理
 *
 * @author xins
 * @date 2024-01-19
 */
@Service
public class WfProcessServiceImpl implements IWfProcessService
{
    @Autowired
    private WfProcessMapper wfProcessMapper;

    /**
     * 查询工单流程
     *
     * @param wfProcessId 工单流程主键
     * @return 工单流程
     */
    @Override
    public WfProcess selectWfProcessByWfProcessId(Long wfProcessId)
    {
        WfProcess wfProcess = wfProcessMapper.selectWfProcessByWfProcessId(wfProcessId);
        Long timeLimit = wfProcess.getTimeLimit();
        if (timeLimit!=null){
        Long days = timeLimit / (24 * 60 * 60l);
        Long hours = timeLimit % (24 * 60 * 60l);
//        System.out.println(days);
//        System.out.println(hours);
            wfProcess.setTimeLimitDays(days);
            wfProcess.setTimeLimitHours(hours/(60*60l));}
        return wfProcess;
    }

    /**
     * 查询工单流程列表
     *
     * @param wfProcess 工单流程
     * @return 工单流程
     */
    @Override
    public List<WfProcess> selectWfProcessList(WfProcess wfProcess)
    {

        List<WfProcess> wfProcesses = wfProcessMapper.selectWfProcessList(wfProcess);

        for (WfProcess wfProcessFor : wfProcesses){
            Long timeLimit = wfProcessFor.getTimeLimit();
            if (timeLimit !=null){
            Long days = timeLimit / (24 * 60 * 60l);
            Long hours = timeLimit % (24 * 60 * 60l);
//            System.out.println(days);
//            System.out.println(hours);
            wfProcessFor.setTimeLimitDays(days);
            wfProcessFor.setTimeLimitHours(hours/(60*60l));
            }
        }
        return wfProcesses;
    }

    /**
     * 新增工单流程
     *
     * @param wfProcess 工单流程
     * @return 结果
     */
    @Override
    public int insertWfProcess(WfProcess wfProcess)
    {
        Long timeLimitDays = wfProcess.getTimeLimitDays();
        wfProcess.setTimeLimit((wfProcess.getTimeLimitDays()*24*60*60)
                +(wfProcess.getTimeLimitHours()*60*60));
        wfProcess.setCreateTime(DateUtils.getNowDate());
        return wfProcessMapper.insertWfProcess(wfProcess);
    }

    /**
     * 修改工单流程
     *
     * @param wfProcess 工单流程
     * @return 结果
     */
    @Override
    public int updateWfProcess(WfProcess wfProcess)
    {
        Long timeLimitDays = wfProcess.getTimeLimitDays();
        wfProcess.setTimeLimit((wfProcess.getTimeLimitDays()*24*60*60)
                +(wfProcess.getTimeLimitHours()*60*60));
        wfProcess.setUpdateTime(DateUtils.getNowDate());
        return wfProcessMapper.updateWfProcess(wfProcess);
    }

    /**
     * 批量删除工单流程
     *
     * @param wfProcessIds 需要删除的工单流程主键
     * @return 结果
     */
    @Override
    public int deleteWfProcessByWfProcessIds(Long[] wfProcessIds)
    {
        return wfProcessMapper.deleteWfProcessByWfProcessIds(wfProcessIds);
    }

    /**
     * 删除工单流程信息
     *
     * @param wfProcessId 工单流程主键
     * @return 结果
     */
    @Override
    public int deleteWfProcessByWfProcessId(Long wfProcessId)
    {
        return wfProcessMapper.deleteWfProcessByWfProcessId(wfProcessId);
    }
}
