package cn.wisenergy.service.impl;

import cn.wisenergy.common.utils.exception.BASE_RESP_CODE_ENUM;
import cn.wisenergy.common.utils.exception.BaseCustomException;
import cn.wisenergy.mapper.WorkSubmitAdoptMapper;
import cn.wisenergy.model.app.WorkSubmitAdopt;
import cn.wisenergy.service.WorkSubmitAdoptService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author chenqi
 * @since 2021-01-19
 */
@Service
@Slf4j
public class WorkSubmitAdoptServiceImpl implements WorkSubmitAdoptService {

    @Autowired
    private WorkSubmitAdoptMapper workSubmitAdoptMapper;

    /**
     * 功能： 获取自动审批和默认填报限制
     *
     * @param id id
     * @return WorkSubmitAdopt
     */
    @Override
    public WorkSubmitAdopt getById(Integer id) {
        log.info("WorkSubmitAdoptServiceImpl[]getById[]input.param.id:" + id);
        if (null == id) {
            throw new BaseCustomException(BASE_RESP_CODE_ENUM.INPUT_PARAM_IS_NULL);
        }
        WorkSubmitAdopt workSubmitAdopt = workSubmitAdoptMapper.selectById(id);
        if (null == workSubmitAdopt) {
            throw new BaseCustomException(BASE_RESP_CODE_ENUM.RESOURCE_NOT_FOUND);
        }
        return workSubmitAdopt;
    }

    @Transactional
    @Override
    public Boolean modifyAuto(WorkSubmitAdopt workSubmitAdopt) {
        log.info("WorkSubmitAdoptServiceImpl[]getById[]input.param.workSubmitAdopt:" + workSubmitAdopt);
        if (null == workSubmitAdopt || (null == workSubmitAdopt.getSubmitTime() && null == workSubmitAdopt.getAutoExamine() && null == workSubmitAdopt.getOrderChange())) {
            throw new BaseCustomException(BASE_RESP_CODE_ENUM.INPUT_PARAM_IS_NULL);
        }
        if (null != workSubmitAdopt.getAutoExamine() && workSubmitAdopt.getAutoExamine() < 1) {
            throw new BaseCustomException(BASE_RESP_CODE_ENUM.THEPERIOD_CANNOT_BELESS_THANONEDAY);
        }
        if (null !=workSubmitAdopt.getSubmitTime() && workSubmitAdopt.getSubmitTime() <1) {
            throw new BaseCustomException(BASE_RESP_CODE_ENUM.THEPERIOD_CANNOT_BELESS_THANONEDAY);
        }
        if (null !=workSubmitAdopt.getOrderChange() && workSubmitAdopt.getOrderChange() <1) {
            throw new BaseCustomException(BASE_RESP_CODE_ENUM.THEPERIOD_CANNOT_BELESS_THANONEDAY);
        }
        workSubmitAdopt.setAutoAdopt(1);
        workSubmitAdopt.setId(1);
        int i = workSubmitAdoptMapper.updateById(workSubmitAdopt);
        return i > 0;
    }

}

