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.WorkTypeMapper;
import cn.wisenergy.model.app.WorkType;
import cn.wisenergy.service.WorkTypeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.Collections;
import java.util.List;

/**
 * @Authotr:陈奇
 * @QQ1799796883
 */
@Slf4j
@Service
public class WorkTypeServiceImpl implements WorkTypeService {


    @Autowired
    private WorkTypeMapper workTypeMapper;


    @Override
    public List<WorkType> getAll(Integer isSort) {
        log.info("WorkTypeServiceImpl[]getAll[]");
        List<WorkType> workTypes = workTypeMapper.getAll();
        if (CollectionUtils.isEmpty(workTypes)) {
            return null;
        }
        if (null != isSort && 1 == isSort) {
            Collections.sort(workTypes);
        }
        return workTypes;
    }

    @Override
    public List<Integer> getIdByReviewer(Integer reviewer) {
        log.info("WorkTypeServiceImpl[]getIdByReviewer[]input.param.reviewer:{}" + reviewer);
        if (null == reviewer) {
            throw new BaseCustomException(BASE_RESP_CODE_ENUM.INPUT_PARAM_IS_NULL);
        }
        List<Integer> ids = workTypeMapper.getIdByReviewer(reviewer);
        if (CollectionUtils.isEmpty(ids)) {
            return null;
        }
        return ids;
    }

    @Transactional
    @Override
    public Boolean addAndModifyWorkType(WorkType workType) {
        log.info("WorkTypeServiceImpl[]modifyTypeById[]input.param.workType:{}" + workType);
        if (null == workType || (StringUtils.isEmpty(workType.getName())  && null == workType.getReviewer())) {
            throw new BaseCustomException(BASE_RESP_CODE_ENUM.INPUT_PARAM_IS_NULL);
        }
        if (null == workType.getId()) {
            if (StringUtils.isEmpty(workType.getName()) || null == workType.getReviewer()) {
                throw new BaseCustomException(BASE_RESP_CODE_ENUM.INPUT_PARAM_IS_NULL);
            }
            int insert = workTypeMapper.insert(workType);
            return insert > 0;
        }else{
            int i = workTypeMapper.updateById(workType);
            if (i == 0) {
                throw new BaseCustomException(BASE_RESP_CODE_ENUM.UPDATE_DATA_FAIL);
            }
            return i>0;
        }
    }

    @Transactional
    @Override
    public Boolean modifyStatusById(Integer id,Integer status) {
        log.info("WorkTypeServiceImpl[]modifyStatusById[]input.param.id:{},status:{}" + id,status);
        if (null == id || null == status) {
            throw new BaseCustomException(BASE_RESP_CODE_ENUM.INPUT_PARAM_IS_NULL);
        }
        int i = workTypeMapper.updateIsStatusById(id, status);
        return i>0;
    }

    @Override
    public List<WorkType> getByIsStatus(Integer isSort,Integer status) {
        log.info("WorkTypeServiceImpl[]getByIsStatus[]");
        List<WorkType> workTypes = workTypeMapper.getByIsStatus(status);
        if (CollectionUtils.isEmpty(workTypes)) {
            return null;
        }
        if (null != isSort && 1 == isSort) {
            Collections.sort(workTypes);
        }
        return workTypes;
    }

}
