package com.chinaweal.aiccs.aiccs.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.chinaweal.aiccs.aiccs.system.entity.DeadlineStas;
import com.chinaweal.aiccs.aiccs.system.mapper.DeadlinestasMapper;
import com.chinaweal.aiccs.aiccs.system.service.DeadlinestasService;
import com.chinaweal.aiccs.common.constant.Constant;
import com.chinaweal.aiccs.common.util.DateUtils;
import com.chinaweal.youfool.framework.springboot.mybatis.plus.BaseServiceImpl;
import com.chinaweal.youfool.framework.springboot.util.AssertUtils;
import org.springframework.stereotype.Service;

import java.time.LocalDate;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Lee
 * @since 2021-10-11
 */
@Service
public class DeadlinestasServiceImpl extends BaseServiceImpl<DeadlinestasMapper, DeadlineStas> implements DeadlinestasService {

    @Override
    public boolean init(String workflowid, LocalDate acceptStartDate) {
        DeadlineStas deadlineStas = new DeadlineStas();
        deadlineStas.setWorkFlowId(workflowid);
        deadlineStas.setAcceptStartDate(acceptStartDate);
        deadlineStas.setAcceptLeftDays(Constant.REPAIR_ILLEGAL_DEADLINE_ACCEPT);
        deadlineStas.setHandleLeftDays(Constant.REPAIR_ILLEGAL_DEADLINE_HANDLE);
        deadlineStas.setApproveLeftDays(Constant.REPAIR_ILLEGAL_DEADLINE_APPROVE);
        int insert = baseMapper.insert(deadlineStas);
        if(insert > 0){
            return true;
        }
        return false;
    }

    @Override
    public boolean update(String workflowid, LocalDate acceptDate, LocalDate handleDate, LocalDate approveDate, boolean isReturn) {
        AssertUtils.isNotBlank(workflowid);
        LambdaQueryWrapper<DeadlineStas> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DeadlineStas::getWorkFlowId,workflowid);
        DeadlineStas deadlineStas = baseMapper.selectOne(wrapper);
        LocalDate nowDate = LocalDate.now();
        boolean flag = false;
        if(deadlineStas != null) {
            if (acceptDate != null) {// 更新经办及审核
                int days = Integer.parseInt(deadlineStas.getAcceptLeftDays());
                if (days >= 0) {
                    int dutyDays = DateUtils.getDutyDays(deadlineStas.getAcceptStartDate(), nowDate);
                    deadlineStas.setAcceptLeftDays((days - dutyDays) + "");
                    deadlineStas.setAcceptStartDate(acceptDate);
                    deadlineStas.setHandleStartDate(acceptDate);//以经办提交时间起算
                }
            }
            if (handleDate != null) {//更新审核
                int days = Integer.parseInt(deadlineStas.getHandleLeftDays());
                if (days >= 0) {
                    int dutyDays = DateUtils.getDutyDays(deadlineStas.getHandleStartDate(), nowDate);
                    deadlineStas.setHandleLeftDays((days - dutyDays) + "");
                    deadlineStas.setHandleStartDate(handleDate);
                }
                if (isReturn) {// 同时更新经办
                    deadlineStas.setAcceptStartDate(handleDate);//以审核回退提交时间起算
                } else {
                    deadlineStas.setApproveStartDate(handleDate);//以审核提交时间起算
                }
            }
            if (approveDate != null) {//更新审批
                int days = Integer.parseInt(deadlineStas.getApproveLeftDays());
                if (days >= 0) {
                    int dutyDays = DateUtils.getDutyDays(deadlineStas.getApproveStartDate(), nowDate);
                    deadlineStas.setApproveLeftDays((days - dutyDays) + "");
                    deadlineStas.setApproveStartDate(approveDate);
                }
                if (isReturn) {// 同时更新经办
                    deadlineStas.setAcceptStartDate(approveDate);//以审核回退提交时间起算
                }
            }
            flag = updateById(deadlineStas);
        }
        return flag;
    }

    @Override
    public DeadlineStas getByWorkflowId(String workflowid) {
        LambdaQueryWrapper<DeadlineStas> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DeadlineStas::getWorkFlowId,workflowid);
        return getOne(wrapper);
    }
}
