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.WorkTypeManagerMapper;
import cn.wisenergy.model.app.WorkTypeManager;
import cn.wisenergy.service.WorkTypeManagerService;
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 org.springframework.util.CollectionUtils;

import java.util.List;

@Slf4j
@Service
public class WorkTypeManagerServiceImpl implements WorkTypeManagerService {

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


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

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

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

    @Override
    public List<Integer> getTypeByGroup() {
        log.info("WorkTypeManagerServiceImpl[]getTypeByGroup[]");
        List<Integer> typeByGroup = workTypeManagerMapper.getTypeByGroup();
        if (CollectionUtils.isEmpty(typeByGroup)) {
            return null;
        }
        return typeByGroup;
    }

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

}
