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 com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
public class WorkTypeServiceImpl implements WorkTypeService {

    @Autowired
    private WorkTypeMapper workTypeMapper;
    /**
     * 功能：根据审核人id获取
     *
     * @param userId
     * @return
     */
    @Override
    public List<WorkType> getByManagerId(Integer userId) {
        log.info("WorkTypeServiceImpl[]getByManagerId[]input.param.userId:{}" + userId);
        return getWorkTypes(userId, "manager_id");
    }


    /**
     * 功能：根据部门id获取
     *
     * @param deptId
     * @return
     */
    @Override
    public List<WorkType> getByDeptId(Integer deptId) {
        log.info("WorkTypeServiceImpl[]getByDeptId[]input.param.deptId:{}" + deptId);
        return getWorkTypes(deptId, "dept_id");
    }

    /**
     * 功能： 根据类型获取
     *
     * @param type
     * @return
     */
    @Override
    public List<WorkType> getByType(Integer type) {
        log.info("WorkTypeServiceImpl[]getByType[]input.param.type:{}" + type);
        return getWorkTypes(type, "type");
    }

    /**
     * 功能：查看有哪些其他Type
     *
     * @return
     */
    @Override
    public List<WorkType> getType() {
        log.info("WorkTypeServiceImpl[]getType");
        QueryWrapper<WorkType> wrapper = new QueryWrapper<>();
        wrapper.groupBy("type");
        wrapper.orderByAsc("type");
        return workTypeMapper.selectList(wrapper);
    }

    private List<WorkType> getWorkTypes(Integer userId, String column) {
        if (null == userId) {
            throw new BaseCustomException(BASE_RESP_CODE_ENUM.INPUT_PARAM_IS_NULL);
        }
        QueryWrapper<WorkType> wrapper = new QueryWrapper<>();
        wrapper.eq(column, userId);
        List<WorkType> workTypes = workTypeMapper.selectList(wrapper);
        return workTypes;
    }

}
