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.mapper.WorkTypeMapper;
import cn.wisenergy.model.app.WorkType;
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;

    @Autowired
    private WorkTypeMapper workTypeMapper;

    /**
     * 功能：根据审核人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");
        List<WorkTypeManager> workTypeManagers = workTypeManagerMapper.selectList(wrapper);
        return addName(workTypeManagers);
    }




    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 addName(workTypeManagers);
    }

    public List<WorkTypeManager> addName(List<WorkTypeManager> workTypeManagers) {
        if (!CollectionUtils.isEmpty(workTypeManagers)) {
            List<WorkType> types = workTypeMapper.getAll();
            if (!CollectionUtils.isEmpty(types)) {
                for (WorkType type : types) {
                    for (WorkTypeManager workTypeManager : workTypeManagers) {
                        if (type.getId().equals(workTypeManager.getType())) {
                            workTypeManager.setName(type.getName());
                        }
                    }
                }
            }
            return workTypeManagers;
        }
        return null;
    }
}
