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.WorkOrderChangeMapper;
import cn.wisenergy.model.dto.DeptOfProjectAndOrderType;
import cn.wisenergy.model.dto.OrderChangeLogMapperDto;
import cn.wisenergy.model.dto.ProjectManagerDto;
import cn.wisenergy.model.dto.WorkOrderChangeLogDto;
import cn.wisenergy.model.enums.LevelEnum;
import cn.wisenergy.model.vo.OrderChangeLogVo;
import cn.wisenergy.model.vo.OrderChangeManagerVo;
import cn.wisenergy.service.WorkOrderChangeService;
import cn.wisenergy.service.WorkProjectService;
import cn.wisenergy.service.utils.UserRoleLevelUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * @Authotr:陈奇
 * @QQ1799796883
 */
@Service
@Slf4j
public class WorkOrderChangeImpl implements WorkOrderChangeService {

    @Autowired
    private WorkOrderChangeMapper workOrderChangeMapper;

    @Autowired
    private WorkProjectService workProjectService;

    @Override
    public PageInfo getOrderChangeLogPage(OrderChangeLogVo vo) {
        log.info("WorkOrderChangeImpl[]getOrderChangeLogPage[]input.param.OrderChangeLogVo:{}", vo);
        vo.setPage(null == vo.getPage() ? 1 : vo.getPage());
        vo.setPageSize(null == vo.getPageSize() ? 10 : vo.getPageSize());
        if (null == vo || null == vo.getUserId()) {
            throw new BaseCustomException(BASE_RESP_CODE_ENUM.INPUT_PARAM_IS_NULL);
        }
        List<Integer> ranks = UserRoleLevelUtils.getRankByUserAndLevelName(vo.getUserId(), LevelEnum.WORKORDER_CHANGERECORD.getLevelName());
        List<Integer> projectIds = new ArrayList<>();
        Set<Integer> deptIds = new HashSet<>();
        Set<Integer> types = new HashSet<>();
        List<DeptOfProjectAndOrderType> projectsAndTypes = workProjectService.getProjectOfFind(vo.getUserId());
        if (CollectionUtils.isEmpty(projectsAndTypes)) {
            return null;
        }
        OrderChangeLogMapperDto mapperDto = getMapperOrderChangeMapperDto(vo, projectsAndTypes,null);
        PageHelper.startPage(vo.getPage(), vo.getPageSize());
        List<WorkOrderChangeLogDto> workOrderChangeLogDto = workOrderChangeMapper.getWorkOrderChangeLogDto(mapperDto);
        PageInfo<WorkOrderChangeLogDto> pageInfo = new PageInfo<>(workOrderChangeLogDto);
        pageInfo.setList(workOrderChangeLogDto);
        return pageInfo;
    }

    @Override
    public OrderChangeLogMapperDto getMapperOrderChangeMapperDto(OrderChangeLogVo vo2, List<DeptOfProjectAndOrderType> projectsAndTypes, OrderChangeManagerVo vo1) {
        List<Integer> projectIds = new ArrayList<>();
        Set<Integer> deptIds = new HashSet<>();
        Set<Integer> types = new HashSet<>();
        OrderChangeLogMapperDto mapperDto = new OrderChangeLogMapperDto();
        if (CollectionUtils.isEmpty(projectsAndTypes)) {
            return null;
        }
        OrderChangeLogVo vo = new OrderChangeLogVo();
        if (null == vo2) {
            BeanUtils.copyProperties(vo1,vo);
        }else{
            vo = vo2;
        }
        if (null == vo.getDeptId()) {
            for (DeptOfProjectAndOrderType projectsAndType : projectsAndTypes) {
                if (!CollectionUtils.isEmpty(projectsAndType.getProjectManagerDtos())) {
                    for (ProjectManagerDto projectManagerDto : projectsAndType.getProjectManagerDtos()) {
                        if (null != projectManagerDto.getId()) {
                            projectIds.add(projectManagerDto.getId());
                        }else{
                            types.add(projectManagerDto.getType());
                            deptIds.add(projectsAndType.getDeptId());
                        }
                    }
                }
            }
        } else if (null == vo.getProjectId() && null == vo.getType()) {
            for (DeptOfProjectAndOrderType projectsAndType : projectsAndTypes) {
                if (projectsAndType.getDeptId().equals(vo.getDeptId())) {
                    if (!CollectionUtils.isEmpty(projectsAndType.getProjectManagerDtos())) {
                        for (ProjectManagerDto projectManagerDto : projectsAndType.getProjectManagerDtos()) {
                            if (null == projectManagerDto.getId()) {
                                types.add(projectManagerDto.getType());
                                deptIds.add(projectsAndType.getDeptId());
                            }else{
                                projectIds.add(projectManagerDto.getId());
                            }

                        }
                    }
                }
            }
        }else{
            if (null != vo.getProjectId()) {
                projectIds.add(vo.getProjectId());
            }else{
                types.add(vo.getType());
                deptIds.add(vo.getDeptId());
            }
        }
        BeanUtils.copyProperties(vo,mapperDto);
        mapperDto.setProjectIds(projectIds);
        mapperDto.setDeptIds(new ArrayList<>(deptIds));
        mapperDto.setTypes(new ArrayList<>(types));
        return mapperDto;
    }
}
