package com.jeeplus.test.rank.service;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jeeplus.sys.service.dto.PostDTO;
import com.jeeplus.sys.utils.StringUtils;
import com.jeeplus.test.jobPosion.service.dto.PositionDTO;
import com.jeeplus.test.rank.domain.*;
import com.jeeplus.test.rank.mapper.*;
import com.jeeplus.test.rank.service.dto.RankDTO;
import com.jeeplus.test.rank.service.dto.RankSystemTableDTO;
import com.jeeplus.test.rank.service.mapstruct.RankRstPositionWrapper;
import com.jeeplus.test.rank.service.mapstruct.RankSystemTableWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

@Service
@Transactional
public class RankSystemTableService extends ServiceImpl<RankSystemTableMapper, RankSystemTable> {

    @Autowired
    private RankSystemTableMapper rankSystemTableMapper;

    @Autowired
    private RankRstPositionMapper rankRstPositionMapper;

    @Autowired
    private RankRstPostMapper rankRstPostMapper;

    @Autowired
    private RankSequenceService rankSequenceService;

    @Autowired
    private RankRstLevelMapper rankRstLevelMapper;

    public IPage<RankSystemTableDTO> findList(Page<RankSystemTableDTO> page, QueryWrapper<RankSystemTable> queryWrapper){

        List<RankSequence> list = rankSequenceService.list(new QueryWrapper<RankSequence>().eq("del_flag", "0").ne("parent_id", "0"));
        if(CollectionUtil.isNotEmpty(list)){
            queryWrapper.eq("rst.del_flag","0").orderByAsc("rs_c.name").orderByAsc("r.group_name");
            return rankSystemTableMapper.findListC(page,queryWrapper);
        }else{
            queryWrapper.eq("rst.del_flag","0").orderByAsc("r.group_name");
            return rankSystemTableMapper.findListP(page,queryWrapper);
        }


    }

    public void saveDate(RankSystemTableDTO rankSystemTableDTO){
        RankSystemTable rankSystemTable = RankSystemTableWrapper.INSTANCE.toEntity(rankSystemTableDTO);
        this.saveOrUpdate(rankSystemTable);
        rankRstPositionMapper.delete(new QueryWrapper<RankRstPosition>().eq("rst_id",rankSystemTable.getId()).eq("rc_id",rankSystemTableDTO.getRcId()).eq("rs_id",rankSystemTableDTO.getRsId()));
        rankRstPostMapper.delete(new QueryWrapper<RankRstPost>().eq("rst_id",rankSystemTable.getId()).eq("rc_id",rankSystemTableDTO.getRcId()).eq("rs_id",rankSystemTableDTO.getRsId()));
        rankRstLevelMapper.delete(new QueryWrapper<RankRstLevel>().eq("rst_id",rankSystemTable.getId()).eq("rc_id",rankSystemTableDTO.getRcId()).eq("rs_id",rankSystemTableDTO.getRsId()));

        if(ObjectUtil.isNotEmpty(rankSystemTableDTO.getRankRstLevelDTO())){
            if(StringUtils.isNotBlank(rankSystemTableDTO.getRankRstLevelDTO().getId())){
                RankRstLevel rankRstLevel = new RankRstLevel();
                rankRstLevel.setLevelId(rankSystemTableDTO.getRankRstLevelDTO().getId());
                rankRstLevel.setRstId(rankSystemTable.getId());
                rankRstLevel.setRcId(rankSystemTableDTO.getRcId());
                rankRstLevel.setRsId(rankSystemTableDTO.getRsId());
                rankRstLevelMapper.insert(rankRstLevel);
            }
        }


        if(CollectionUtil.isNotEmpty(rankSystemTableDTO.getRankRstPositionDTOIdList())){
            rankSystemTableDTO.getRankRstPositionDTOIdList().stream().forEach(item->{
                RankRstPosition rankRstPosition = new RankRstPosition();
                rankRstPosition.setPositionId(item);
                rankRstPosition.setRstId(rankSystemTable.getId());
                rankRstPosition.setRcId(rankSystemTableDTO.getRcId());
                rankRstPosition.setRsId(rankSystemTableDTO.getRsId());
                rankRstPositionMapper.insert(rankRstPosition);
            });
        }

        if(CollectionUtil.isNotEmpty(rankSystemTableDTO.getRankRstPostDTOIdList())){
            rankSystemTableDTO.getRankRstPostDTOIdList().stream().forEach(item->{
                RankRstPost rankRstPost = new RankRstPost();
                rankRstPost.setPostId(item);
                rankRstPost.setRstId(rankSystemTable.getId());
                rankRstPost.setRcId(rankSystemTableDTO.getRcId());
                rankRstPost.setRsId(rankSystemTableDTO.getRsId());
                rankRstPostMapper.insert(rankRstPost);
            });
        }
    }

    public RankSystemTableDTO queryById(String rankId,String rcId,String rsId){
        if(StrUtil.isEmpty(rankId) || StrUtil.isEmpty(rcId) || StrUtil.isEmpty(rsId)){
            return null;
        }
        List<RankSequence> rankSequenceList = rankSequenceService.list(new QueryWrapper<RankSequence>().eq("id", rsId).eq("parent_id","0"));
        if(CollectionUtil.isNotEmpty(rankSequenceList)){
            RankSystemTableDTO rankSystemTableDTO = rankSystemTableMapper.queryByIdP(rankId, rcId, rsId);
            List<String> positionIds = rankSystemTableDTO.getRankRstPositionDTOList().stream().map(PositionDTO::getId).collect(Collectors.toList());
            List<String> postIds = rankSystemTableDTO.getRankRstPostDTOList().stream().map(PostDTO::getId).collect(Collectors.toList());
            rankSystemTableDTO.setRankRstPositionDTOIdList(positionIds);
            rankSystemTableDTO.setRankRstPostDTOIdList(postIds);
            return rankSystemTableDTO;
        }else{
            RankSystemTableDTO rankSystemTableDTO = rankSystemTableMapper.queryByIdC(rankId,rcId,rsId);
            List<String> positionIds = rankSystemTableDTO.getRankRstPositionDTOList().stream().map(PositionDTO::getId).collect(Collectors.toList());
            List<String> postIds = rankSystemTableDTO.getRankRstPostDTOList().stream().map(PostDTO::getId).collect(Collectors.toList());
            rankSystemTableDTO.setRankRstPositionDTOIdList(positionIds);
            rankSystemTableDTO.setRankRstPostDTOIdList(postIds);
            return rankSystemTableDTO;
        }

    }
}
