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

import java.util.List;
import com.hw.common.core.utils.DateUtils;
import com.hw.system.api.domain.common.WfProcessActivity;
import com.hw.system.api.domain.common.WfProcessActivityAuth;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.hw.common.core.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.hw.system.common.mapper.WfProcessActivityMapper;
import com.hw.system.common.service.IWfProcessActivityService;

/**
 * 工单流程节点步骤Service业务层处理
 *
 * @author xins
 * @date 2024-01-19
 */
@Service
public class WfProcessActivityServiceImpl implements IWfProcessActivityService
{
    @Autowired
    private WfProcessActivityMapper wfProcessActivityMapper;

    /**
     * 查询工单流程节点步骤
     *
     * @param processActivityId 工单流程节点步骤主键
     * @return 工单流程节点步骤
     */
    @Override
    public WfProcessActivity selectWfProcessActivityByProcessActivityId(Long processActivityId)
    {

        WfProcessActivity wfProcessActivity = wfProcessActivityMapper.selectWfProcessActivityByProcessActivityId(processActivityId);
        Long timeLimit = wfProcessActivity.getTimeLimit();
        if (timeLimit !=null){
            Long days = timeLimit / (24 * 60 * 60l);
            Long hours = timeLimit % (24 * 60 * 60l);
//            System.out.println(days);
//            System.out.println(hours);
            wfProcessActivity.setTimeLimitDays(days);
            wfProcessActivity.setTimeLimitHours(hours/(60*60l));
        }
        return wfProcessActivity;
    }

    /**
     * 查询工单流程节点步骤列表
     *
     * @param wfProcessActivity 工单流程节点步骤
     * @return 工单流程节点步骤
     */
    @Override
    public List<WfProcessActivity> selectWfProcessActivityList(WfProcessActivity wfProcessActivity)
    {
        List<WfProcessActivity> wfProcessActivities = wfProcessActivityMapper.selectWfProcessActivityList(wfProcessActivity);
        for (WfProcessActivity wfProcessFor : wfProcessActivities){
            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 wfProcessActivities;
    }

    /**
     * 新增工单流程节点步骤
     *
     * @param wfProcessActivity 工单流程节点步骤
     * @return 结果
     */
    @Transactional
    @Override
    public int insertWfProcessActivity(WfProcessActivity wfProcessActivity)
    {
        wfProcessActivity.setCreateTime(DateUtils.getNowDate());
        int rows = wfProcessActivityMapper.insertWfProcessActivity(wfProcessActivity);
        insertWfProcessActivityAuth(wfProcessActivity);
        return rows;
    }

    /**
     * 修改工单流程节点步骤
     *
     * @param wfProcessActivity 工单流程节点步骤
     * @return 结果
     */
    @Transactional
    @Override
    public int updateWfProcessActivity(WfProcessActivity wfProcessActivity)
    {
        wfProcessActivity.setUpdateTime(DateUtils.getNowDate());
        wfProcessActivityMapper.deleteWfProcessActivityAuthByProcessActivityId(wfProcessActivity.getProcessActivityId());
        insertWfProcessActivityAuth(wfProcessActivity);



        Long timeLimitDays = wfProcessActivity.getTimeLimitDays();
        wfProcessActivity.setTimeLimit((wfProcessActivity.getTimeLimitDays()*24*60*60)
                +(wfProcessActivity.getTimeLimitHours()*60*60));
        wfProcessActivity.setUpdateTime(DateUtils.getNowDate());
        return wfProcessActivityMapper.updateWfProcessActivity(wfProcessActivity);
    }

    /**
     * 批量删除工单流程节点步骤
     *
     * @param processActivityIds 需要删除的工单流程节点步骤主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteWfProcessActivityByProcessActivityIds(Long[] processActivityIds)
    {
        wfProcessActivityMapper.deleteWfProcessActivityAuthByProcessActivityIds(processActivityIds);
        return wfProcessActivityMapper.deleteWfProcessActivityByProcessActivityIds(processActivityIds);
    }

    /**
     * 删除工单流程节点步骤信息
     *
     * @param processActivityId 工单流程节点步骤主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteWfProcessActivityByProcessActivityId(Long processActivityId)
    {
        wfProcessActivityMapper.deleteWfProcessActivityAuthByProcessActivityId(processActivityId);
        return wfProcessActivityMapper.deleteWfProcessActivityByProcessActivityId(processActivityId);
    }

    @Override
    public Long selectProcessActivityIdByWfProcessId(Long wfProcessId) {
        return wfProcessActivityMapper.selectProcessActivityIdByWfProcessId(wfProcessId);
    }

    /**
     * 新增工单流程节点步骤权限信息
     *
     * @param wfProcessActivity 工单流程节点步骤对象
     */
    public void insertWfProcessActivityAuth(WfProcessActivity wfProcessActivity)
    {
        List<WfProcessActivityAuth> wfProcessActivityAuthList = wfProcessActivity.getWfProcessActivityAuthList();
        Long processActivityId = wfProcessActivity.getProcessActivityId();
        if (StringUtils.isNotNull(wfProcessActivityAuthList))
        {
            List<WfProcessActivityAuth> list = new ArrayList<WfProcessActivityAuth>();
            for (WfProcessActivityAuth wfProcessActivityAuth : wfProcessActivityAuthList)
            {
                wfProcessActivityAuth.setProcessActivityId(processActivityId);
                list.add(wfProcessActivityAuth);
            }
            if (list.size() > 0)
            {
                wfProcessActivityMapper.batchWfProcessActivityAuth(list);
            }
        }
    }



    /**
     * 查询工单流程节点步骤
     *
     * @param wfProcessId            工单流程主键
     * @param processActivityOrder 顺序
     * @return 工单流程节点步骤
     */
    public WfProcessActivity selectWfProcessActivityByUI(Long wfProcessId, Long processActivityOrder){
        return wfProcessActivityMapper.selectWfProcessActivityByUI(wfProcessId, processActivityOrder);
    }
}
