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.WorkCentreMapper;
import cn.wisenergy.mapper.WorkDeptMapper;
import cn.wisenergy.model.app.WorkCentre;
import cn.wisenergy.model.app.WorkDept;
import cn.wisenergy.model.app.WorkUser;
import cn.wisenergy.service.WorkCentreService;
import cn.wisenergy.service.WorkDeptService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;

/**
 * @Authotr:陈奇
 * @QQ1799796883
 */

@Service
@Slf4j
public class WorkCentreServiceImpl implements WorkCentreService {

    @Autowired
    private WorkCentreMapper workCentreMapper;

    @Autowired
    private WorkDeptService workDeptService;

    @Autowired
    private WorkDeptMapper workDeptMapper;
    @Override
    public WorkCentre getCentreById(Integer id) {
        log.info("WorkCentreServiceImpl[]getCentreById[]input.param.id:{}" + id);
        if (null == id) {
            throw new BaseCustomException(BASE_RESP_CODE_ENUM.INPUT_PARAM_IS_NULL);
        }
        WorkCentre workCentre = workCentreMapper.selectById(id);

        if (null != workCentre) {
            return workCentre;
        }
        return null;
    }

    @Override
    public Boolean updateManagerIdIsNullById(Integer id) {
        log.info("WorkCentreServiceImpl[]updateManagerIdIsNullById[]input.param.id" + id);
        if (null == id) {
            throw new BaseCustomException(BASE_RESP_CODE_ENUM.INPUT_PARAM_IS_NULL);
        }
        int i = workCentreMapper.updateManagerIdIsNullById(id);
        return i>0;
    }

    @Override
    public Boolean updateById(WorkCentre workCentre) {
        log.info("WorkCentreServiceImpl[]updateById[]input.param.workCentre" + workCentre);
        if (null == workCentre || null == workCentre.getId()) {
            throw new BaseCustomException(BASE_RESP_CODE_ENUM.INPUT_PARAM_IS_NULL);
        }
        int updateRow = workCentreMapper.updateById(workCentre);
        return updateRow>0;
    }

    @Override
    public List<WorkDept> getDeptByCentreManager(WorkUser user) {
        log.info("WorkCentreServiceImpl[]getDeptByCentreManagerId[]input.param.user" + user);
        if (null == user) {
            throw new BaseCustomException(BASE_RESP_CODE_ENUM.INPUT_PARAM_IS_NULL);
        }
        Integer centreId = workDeptService.getById(user.getDeptId()).getCentreId();
        WorkCentre workCentre = workCentreMapper.selectById(centreId);
        HashMap<String, Object> map = new HashMap<>(5);
        map.put("centre_id", workCentre.getId());
        return workDeptMapper.getDeptByCondition(map);
    }
}
