package jnpf.cultivate.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jnpf.cultivate.mapper.FtbCultivatePromotionMemberMapper;
import jnpf.cultivate.mapper.FtbCultivatePromotionPostMapper;
import jnpf.cultivate.service.FtbCultivatePromotionMemberService;
import jnpf.cultivate.service.FtbCultivatePromotionPostService;
import jnpf.cultivate.utils.UserApiV2Util;
import jnpf.model.cultivate.dto.promotion.FtbCultivatePromotionForAppDto;
import jnpf.model.cultivate.dto.promotion.FtbCultivatePromotionMemberDto;
import jnpf.model.cultivate.po.promotion.FtbCultivatePromotionMember;
import jnpf.model.cultivate.po.promotion.FtbCultivatePromotionPost;
import jnpf.model.cultivate.vo.promotion.FtbCultivatePromotionForAppVO;
import jnpf.model.cultivate.vo.promotion.FtbCultivatePromotionMemberVO;
import jnpf.model.cultivate.vo.promotion.FtbCultivatePromotionPostVO;
import jnpf.model.cultivate.vo.promotion.FtbCultivatePromotionVO;
import jnpf.permission.UserApi;
import jnpf.permission.model.user.UserInfoVO;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@Service
@RequiredArgsConstructor
public class FtbCultivatePromotionPostServiceImpl extends ServiceImpl<FtbCultivatePromotionPostMapper, FtbCultivatePromotionPost> implements FtbCultivatePromotionPostService {

    private final FtbCultivatePromotionMemberMapper memberMapper;

    private final FtbCultivatePromotionMemberService memberService;
    private final UserApiV2Util userApiV2Util;

    @Override
    public void savePost(FtbCultivatePromotionPost creatDto) {
        baseMapper.insert(creatDto);

    }

    @Override
    public List<FtbCultivatePromotionPost> getList(String promotionId) {
        QueryWrapper<FtbCultivatePromotionPost> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("F_PromotionId", promotionId);
        queryWrapper.orderByAsc("F_Level");
        return baseMapper.selectList(queryWrapper);
    }

    @Override
    public void updatePost(FtbCultivatePromotionPost dto) {
        baseMapper.updateById(dto);
    }

    @Override
    public FtbCultivatePromotionForAppVO queryThePromotedChannelsThatAreInitiated(String userId, String postId) {
        FtbCultivatePromotionForAppVO result = new FtbCultivatePromotionForAppVO();
        // 根据userId查询已启用的晋升通道
        FtbCultivatePromotionVO promotionVO = memberMapper.queryPromotionByUser(userId, postId);
        String promotionVOId=null;
        if (promotionVO != null) {
            result.setOptPro(promotionVO);
            //已启用的id
            promotionVOId = promotionVO.getId();
        }
        // 根据岗位查询相同岗位可选的晋升通道
        List<FtbCultivatePromotionVO>  baseList = baseMapper.queryPromonChanOfTheSamePosition(postId, promotionVOId);
        result.setOtherOptPro(baseList);
        return result;
    }

    @Override
    public String changeTheEnablementStatus(FtbCultivatePromotionForAppDto forAppDto) {
        // 标记是否为下属设置启用晋升通道
        FtbCultivatePromotionMember promotionMember = getFtbCultivatePromotionMember(forAppDto);
        if ("1".equals(forAppDto.getFlag())) {
            // 当前这个成员没有开启晋升通道
            if (promotionMember == null){
                newPromotionChannels(forAppDto);
                return null;
            }
        }else {
            if (promotionMember == null) {
                newPromotionChannels(forAppDto);
                return null;
            }
            if (1 == promotionMember.getState()) {
                //您好，若想更换晋升通道，请与您的直接主管“主管姓名”联系变更修改。
                UserInfoVO bossByUserId = userApiV2Util.getBossByUserId(forAppDto.getUserId(),null);
                return ("您好，若想更换晋升通道，请与您的直接主管“" +bossByUserId.getRealName() + "”联系变更修改。");
            }
        }
        // 更改当前晋升通道
        // 更换启用其他的晋升通道
        promotionMember.setPromotionId(forAppDto.getPromotionId());
        promotionMember.setEnableStatus(1);
        promotionMember.setUserId(forAppDto.getUserId());
        memberMapper.updateById(promotionMember);
        return null;
    }

    /**
     * 用户自己启用晋升通道
     * @param forAppDto
     */
    private void newPromotionChannels(FtbCultivatePromotionForAppDto forAppDto) {
        FtbCultivatePromotionMemberDto dto = new FtbCultivatePromotionMemberDto();
        List<String> strings = Collections.singletonList(forAppDto.getUserId());
        List<FtbCultivatePromotionMemberDto.PromotionUser> userIds = new ArrayList<>();
        FtbCultivatePromotionMemberDto.PromotionUser promotionUser = new FtbCultivatePromotionMemberDto.PromotionUser();
        promotionUser.setUserId(forAppDto.getUserId());
        promotionUser.setUserName(forAppDto.getUserName());
        promotionUser.setOrgId(forAppDto.getOrgId());
        promotionUser.setOrgName(forAppDto.getOrgName());
        promotionUser.setPositionId(forAppDto.getPositionId());
        promotionUser.setPositionName(forAppDto.getPositionName());
        promotionUser.setGradeId(forAppDto.getGradeId());
        promotionUser.setGradeName(forAppDto.getGradeName());
        userIds.add(promotionUser);
        dto.setUserIds(userIds);
        dto.setPromotionId(forAppDto.getPromotionId());
        memberService.addMembersToProChannel(dto);
    }

    @Override
    public void changeWhetherTheChangeIsAllowed(FtbCultivatePromotionForAppDto forAppDto) {
        FtbCultivatePromotionMember promotionMember = getFtbCultivatePromotionMember(forAppDto);
        // 查询是否为下属
        if (promotionMember == null ){
            throw new RuntimeException("当前用户暂时没有开启晋升通道请核对后重试");
        }
        promotionMember.setState(forAppDto.getState());
        memberMapper.updateById(promotionMember);
    }

    @Override
    public boolean checkWhetherThePromotionChannelIsEnabled(String userId, String postId) {
        FtbCultivatePromotionVO promotionVO = memberMapper.queryPromotionByUser(userId, postId);
        if (ObjectUtil.isNotEmpty(promotionVO)) {
            return true;
        }
        return false;
    }

    @Override
    public Integer userIsAllowed(String userId) {
        FtbCultivatePromotionMemberVO promotionMember = memberMapper.queryUserInfo(userId);
        // 默认是可以启用晋升通道
        if (promotionMember == null){
            return 0;
        }
        return promotionMember.getState();
    }

    @Override
    public List<FtbCultivatePromotionPostVO> queryPostHasExist() {
        return memberMapper.queryPostHasExist();
    }

    private FtbCultivatePromotionMember getFtbCultivatePromotionMember(FtbCultivatePromotionForAppDto forAppDto) {
        //查询出当前启用的晋升通道id
        LambdaQueryWrapper<FtbCultivatePromotionMember> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(FtbCultivatePromotionMember::getUserId, forAppDto.getUserId());
        return  memberMapper.selectOne(queryWrapper);
    }
}
