package cn.com.ty.lift.system.homepage.service;

import cn.com.ty.lift.common.constants.ApiConstants;
import cn.com.ty.lift.common.constants.CommonEnum;
import cn.com.ty.lift.common.utils.ProjectUtils;
import cn.com.ty.lift.system.constants.CommonConstants;
import cn.com.ty.lift.system.homepage.dao.dto.request.TodoRequest;
import cn.com.ty.lift.system.homepage.dao.dto.response.*;
import cn.com.ty.lift.system.homepage.dao.mapper.TodoDataMapper;
import cn.com.ty.lift.system.settings.dao.entity.MtCompanyAttestation;
import cn.com.ty.lift.system.settings.service.IMtCompanyAttestationService;
import cn.com.ty.lift.system.user.dao.entity.MtCompanyUser;
import cn.com.ty.lift.system.user.dao.entity.UserApplication;
import cn.com.ty.lift.system.user.service.IMtCompanyUserService;
import cn.com.ty.lift.system.user.service.IUserApplicationService;
import cn.com.xwy.boot.web.dto.RestResponse;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author huangyuan
 * @date 2020/2/14
 * @description
 */
@Service
public class TodoService {

    @Resource
    private TodoDataMapper todoDataMapper;

    @Resource
    private IMtCompanyAttestationService mtCompanyAttestationService;

    @Resource
    private IUserApplicationService userApplicationService;

    @Resource
    private IMtCompanyUserService mtCompanyUserService;

    /**
     * @param todoRequest 公司id
     * @return
     * @description 待办数量
     * @date 2020/2/14 10:55 上午
     */
    public RestResponse todoNum(TodoRequest todoRequest) {
        handleTodoRequest(todoRequest);
        Map<Integer, Long> todoTypeToNum = CommonConstants.TODO_TYPE_TO_NUM;
        //1.设置行政待办事项数量
        //团队成员申请待办
        long administrativeTodoCount = userApplicationService.count(new QueryWrapper<UserApplication>()
                .eq("status", ApiConstants.ApplicationConstants.APPLY_WAIT)
                .eq("mt_company_id", todoRequest.getMtCompanyId())
        );
        //公司申请待办,平台公司才能有公司申请待办
        if (todoRequest.getMtCompanyId().equals(ApiConstants.PLATFORM_COMPANY_ID)) {
            long platCompanyTodoCount = mtCompanyAttestationService.count(new QueryWrapper<MtCompanyAttestation>()
                    .eq("is_certificated", ApiConstants.CompanyConstants.MAINTENANCE_WAIT_CERTIFICATE)
            );
            administrativeTodoCount = administrativeTodoCount + platCompanyTodoCount;
        }
        todoTypeToNum.put(CommonConstants.TodoConstants.ADMINISTRATIVE_TODO, administrativeTodoCount);
        //2.设置急修待办数量
        long emergencyTodoCount = todoDataMapper.getEmergencyTodoCount(todoRequest);
        todoTypeToNum.put(CommonConstants.TodoConstants.EMERGENCY_TODO, emergencyTodoCount);
        //3.设置维保待办数量
        long maintenanceTodoCount = todoDataMapper.getMaintenanceTodoCount(todoRequest);
        todoTypeToNum.put(CommonConstants.TodoConstants.MAINTENANCE_TODO, maintenanceTodoCount);
        //4.设置大修待办数量
        long capitalRepairTodoCount = todoDataMapper.getCapitalTodoCount(todoRequest);
        todoTypeToNum.put(CommonConstants.TodoConstants.CAPITAL_REPAIR_TODO, capitalRepairTodoCount);
        //5.设置年检待办数量
        long annualTodoCount = todoDataMapper.getAnnualTodoCount(todoRequest);
        todoTypeToNum.put(CommonConstants.TodoConstants.ANNUAL_INSPECTION_TODO, annualTodoCount);
        return RestResponse.success(ProjectUtils.transReturnMapToStatisticsKVModel(todoTypeToNum),
                ApiConstants.RESULT_SUCCESS, "获取待办数据成功");
    }

    /**
     * @param
     * @return
     * @description 团队申请团队待办
     * @date 2020/2/24 11:31 上午
     */
    public RestResponse companyApplyTodo(TodoRequest todoRequest) {
        List<CompanyApplyResponse> companyApplyResponseList = todoDataMapper.getCompanyApplyTodo(todoRequest);
        //设置团队人数
        if (companyApplyResponseList != null && companyApplyResponseList.size() > 0) {
            setCompanyUserNum(companyApplyResponseList);
        }
        return RestResponse.success(companyApplyResponseList, ApiConstants.RESULT_SUCCESS, "获取待办数据成功");
    }

    /**
     * @param companyApplyResponseList 团队申请信息
     * @return
     * @description 设置团队人数
     * @date 2020/2/24 12:05 下午
     */
    private void setCompanyUserNum(List<CompanyApplyResponse> companyApplyResponseList) {
        List<Long> companyIdList = companyApplyResponseList.stream()
                .map(CompanyApplyResponse::getMtCompanyId).collect(Collectors.toList());
        //获取团队用户
        List<MtCompanyUser> mtCompanyUserList = mtCompanyUserService.list(new QueryWrapper<MtCompanyUser>()
                .in(companyIdList != null && companyIdList.size() > 0,
                        "mt_company_id", companyIdList)
        );
        if (mtCompanyUserList != null && mtCompanyUserList.size() > 0) {
            Map<Long, Long> companyIdToUserNum = mtCompanyUserList.stream()
                    .collect(Collectors.groupingBy(MtCompanyUser::getMtCompanyId, Collectors.counting()));
            //设置团队人数
            companyApplyResponseList.forEach(companyApplyResponse -> {
                if (companyIdToUserNum.size() > 0) {
                    long userNum = companyIdToUserNum.get(companyApplyResponse.getMtCompanyId()) != null ?
                            companyIdToUserNum.get(companyApplyResponse.getMtCompanyId()) : 0L;
                    companyApplyResponse.setCompanyUserNum(userNum);
                }
            });
        }
    }

    /**
     * @param todoRequest 角色编码
     * @description 通过角色来设置id值
     * @date 2020/4/25 10:01 上午
     */
    private void handleTodoRequest(TodoRequest todoRequest) {
        if (CommonEnum.DefaultRole.REGION_DIRECTOR.getCode().equals(todoRequest.getCurrentRoleCode())) {
            todoRequest.setRegionChargeId(todoRequest.getUserId());
        }
        if(CommonEnum.DefaultRole.CLERK.getCode().equals(todoRequest.getCurrentRoleCode())) {
            todoRequest.setClerkId(todoRequest.getUserId().toString());
        }
    }
}
