/*
 * Author: wangjian
 * 生成ServiceImpl
 */
package com.tofly.gpsboot.service.maintain.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tofly.api.DepartmentClient;
import com.tofly.api.UserClent;
import com.tofly.common.oauth.util.SecurityUtils;
import com.tofly.gpsboot.common.Constant;
import com.tofly.gpsboot.common.system.AuditTypeEnum;
import com.tofly.pojo.maintain.dto.RepairplandetailDto;
import com.tofly.pojo.maintain.vo.RepairplandetailVo;
import com.tofly.gpsboot.mapper.maintain.RepairplandetailMapper;
import com.tofly.gpsboot.service.comm.IAuditService;
import com.tofly.gpsboot.service.maintain.RepairorderService;
import com.tofly.gpsboot.service.maintain.RepairplanService;
import com.tofly.gpsboot.service.maintain.RepairplandetailService;
import com.tofly.gpsboot.service.maintain.RepairtypeService;
import com.tofly.pojo.User;
import com.tofly.pojo.comm.Audit;
import com.tofly.pojo.maintain.Repairorder;
import com.tofly.pojo.maintain.Repairplan;
import com.tofly.pojo.maintain.Repairplandetail;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;


/**
 * 抢维修年度计划内容信息表
 *
 * @author admin
 * @date Tue Apr 06 00:00:00 CST 2021
 */
@Service("repairplandetailService")
public class RepairplandetailServiceImpl extends ServiceImpl<RepairplandetailMapper, Repairplandetail> implements RepairplandetailService {
    @Autowired
    private UserClent userClent;

    @Autowired
    private RepairorderService repairorderService;

    @Autowired
    private RepairtypeService repairtypeService;

    @Autowired
    private IAuditService iAuditService;

    @Autowired
    private DepartmentClient departmentClient;

    @Autowired
    private RepairplanService repairplanService;

    @Override
    public List<RepairplandetailVo> viewConversion(List<Repairplandetail> infoList) {
        List<RepairplandetailVo> infoVoList = new ArrayList<>();
        infoList.forEach(info -> {
            RepairplandetailVo infoVo = JSONObject.parseObject(JSON.toJSONString(info), RepairplandetailVo.class);
            Long respUser = info.getRespUser();
            // 添加维修类型名称/任务接收负责人名称
            infoVo.setTypeName(repairtypeService.getById(info.getTypeId()).getName());
            if (null != respUser && 0 != respUser) {
                User userAudit = userClent.getById(respUser).getResult();
                if(null!=userAudit){
                    infoVo.setRespUserName(userAudit.getRealName());
                }
            }
            // 返回前端工单id,若不存在则为空
            List<Repairorder> list = repairorderService.list(new QueryWrapper<Repairorder>().lambda().eq(Repairorder::getYearId, info.getYearId()).eq(Repairorder::getMonth, info.getMonth()));
            if (null != list && list.size() > 0) {
                infoVo.setOrderId(list.get(0).getId().toString());
            } else {
                infoVo.setOrderId("");
            }
            infoVoList.add(infoVo);
        });
        return infoVoList;
    }

    @Override
    public Page<RepairplandetailVo> getRepairPlanPage(Page page, RepairplandetailDto parDto) {
        String year = parDto.getYear();
        String month = parDto.getMonth();
        Long respUser = parDto.getRespUser();
        Long typeId = parDto.getTypeId();
        String typeIds = parDto.getTypeIds();
        QueryWrapper<Repairplandetail> queryWrapper = new QueryWrapper<>();
        // 当前部门下才能看到对应数据
        Set<Long> userIdList = departmentClient.getDeptUserList(SecurityUtils.getUserDeptId()).getResult().stream().map(User::getId).collect(Collectors.toSet());
        // 获取当前用户列表创建的年计划id列表
        Set<Long> userPlanIdList = repairplanService.list(new QueryWrapper<Repairplan>().lambda().in(Repairplan::getCreateUser, userIdList)).stream().map(Repairplan::getId).collect(Collectors.toSet());

        if (!StringUtils.isEmpty(year)) {
            queryWrapper.eq("YEAR", year);
        }
        if (!StringUtils.isEmpty(month)) {
            queryWrapper.eq("MONTH", month);
        }
        if (null != respUser) {
            queryWrapper.eq("RESP_USER", respUser);
        }
        if (null != typeId) {
            queryWrapper.eq("TYPE_ID", typeId);
        }
        if(!StringUtils.isEmpty(typeIds)){
            List<String> stateList = Arrays.stream(typeIds.split(Constant.CSV)).map(String::trim)
                    .collect(Collectors.toList());
            queryWrapper.in("TYPE_ID", stateList);
        }
        queryWrapper.orderByDesc("YEAR").orderByDesc("MONTH");
        // 由于分管经理不属于当前部门,需要增加到当前查询用户列表
        Set<Long> collect = iAuditService.list(new QueryWrapper<Audit>().lambda().eq(Audit::getAuditType, AuditTypeEnum.AUDIT_TYPE_YEAR_PLAN.getCode())).stream().map(Audit::getAuditPlanId).collect(Collectors.toSet());

        userPlanIdList.addAll(collect);
        Long yearId = parDto.getYearId();
        if (null != yearId && 0 != yearId) {
            queryWrapper.eq("YEAR_ID", yearId);
        } else {
            if (userPlanIdList.size() > 0) {
                queryWrapper.in("YEAR_ID", userPlanIdList);
            } else {
                queryWrapper.in("YEAR_ID", 0);
            }
        }
        Page infoPageList = this.page(page, queryWrapper);
        infoPageList.setRecords(this.viewConversion(infoPageList.getRecords()));
        return infoPageList;
    }
}
