package com.elink.esua.epdc.modules.analysis.service.impl;


import cn.binarywang.wx.miniapp.bean.WxMaJscode2SessionResult;
import com.elink.esua.epdc.common.token.dto.WorkUserAuthorizationDTO;
import com.elink.esua.epdc.common.token.dto.WorkUserLoginDTO;
import com.elink.esua.epdc.common.token.dto.WorkUserTokenFormDTO;
import com.elink.esua.epdc.commons.tools.constant.NumConstant;
import com.elink.esua.epdc.commons.tools.exception.RenException;
import com.elink.esua.epdc.commons.tools.utils.ConvertUtils;
import com.elink.esua.epdc.commons.tools.utils.Result;
import com.elink.esua.epdc.dto.SysUserDTO;
import com.elink.esua.epdc.dto.form.EpdcWorkAppUserMaTokenFormDTO;
import com.elink.esua.epdc.dto.user.form.MemberRankFormDTO;
import com.elink.esua.epdc.dto.user.form.UserCountDTO;
import com.elink.esua.epdc.dto.user.form.UserSortRankFormDTO;
import com.elink.esua.epdc.dto.user.result.OldMemberRankResultDTO;
import com.elink.esua.epdc.dto.user.result.UserSortRankResultDTO;
import com.elink.esua.epdc.dto.user.result.YoungMemberRankResultDTO;
import com.elink.esua.epdc.feign.AdminFeignClient;
import com.elink.esua.epdc.modules.analysis.feign.AnalysisUserFeignClient;
import com.elink.esua.epdc.feign.ResourceFeignClient;
import com.elink.esua.epdc.modules.analysis.service.AnalysisUserService;
import com.elink.esua.epdc.utils.WxMaServiceUtils;
import me.chanjar.weixin.common.error.WxErrorException;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 项目模块-移动app端
 * @Author LPF
 * @Date 2019/11/18 16:34
 */
@Service
public class AnalysisUserServiceImpl implements AnalysisUserService {

    @Autowired
    private AnalysisUserFeignClient analysisUserFeignClient;

    @Autowired
    private WxMaServiceUtils wxMaServiceUtils;

    @Autowired
    private ResourceFeignClient resourceFeignClient;

    @Autowired
    private AdminFeignClient adminFeignClient;

    @Override
    public Result test() {
        return analysisUserFeignClient.test();
    }

    @Override
    public Result<WorkUserAuthorizationDTO> login(WorkUserLoginDTO formDto) {
        // 获取openId
        String openId = this.getWxMaUser(formDto.getWxCode()).getOpenid();
        formDto.setOpenId(openId);
        return resourceFeignClient.workLogin(formDto);
    }

    @Override
    public Result<WorkUserAuthorizationDTO> getMaToken(EpdcWorkAppUserMaTokenFormDTO formDto) {
        // 获取openId
        String openId = this.getWxMaUser(formDto.getWxCode()).getOpenid();

        // 检查当前微信是否已绑定账户
        Result<SysUserDTO> sysUserDtoResult = adminFeignClient.getSysUserInfoByOpenId(openId);
        if (!sysUserDtoResult.success()) {
            return new Result<WorkUserAuthorizationDTO>().error(sysUserDtoResult.getMsg());
        }
        SysUserDTO userDto = sysUserDtoResult.getData();
        if (null == userDto) {
            WorkUserAuthorizationDTO resultDto = new WorkUserAuthorizationDTO();
            resultDto.setRegisterState(NumConstant.ZERO_STR);
            return new Result<WorkUserAuthorizationDTO>().ok(resultDto);
        }

        return resourceFeignClient.getToken(ConvertUtils.sourceToTarget(userDto, WorkUserTokenFormDTO.class));
    }

    @Override
    public Result<List<OldMemberRankResultDTO>> listOldMemberRankByDeptId( MemberRankFormDTO formDto) {
        return analysisUserFeignClient.listOldMemberRankByDeptId(formDto);
    }

    @Override
    public Result<List<YoungMemberRankResultDTO>> listYoungMemberRankByDeptId(MemberRankFormDTO formDto) {
        return analysisUserFeignClient.listYoungMemberRankByDeptId(formDto);
    }
    @Override
    public Result<List<UserSortRankResultDTO>> userRegisterSort(UserSortRankFormDTO formDto){
        return analysisUserFeignClient.userRegisterSort(formDto);
    }



    @Override
    public Result getGridStatistics(UserCountDTO formDto) {
        return analysisUserFeignClient.getGridStatistics(formDto);
    }

    @Override
    public Result getUserStatistics(UserCountDTO formDto) {
        return analysisUserFeignClient.getUserStatistics(formDto);
    }


    /**
     *
     * 解析微信code获取小程序用户信息
     *
     * @params [wxCode]
     * @return cn.binarywang.wx.miniapp.bean.WxMaJscode2SessionResult
     * @author liuchuang
     * @since 2020/2/12 13:20
     */
    private WxMaJscode2SessionResult getWxMaUser(String wxCode) {
        WxMaJscode2SessionResult wxMaJscode2SessionResult = null;
        try {
            wxMaJscode2SessionResult = wxMaServiceUtils.analysisWxMaService().jsCode2SessionInfo(wxCode);
        } catch (WxErrorException e) {
            e.printStackTrace();
        }
        if (null == wxMaJscode2SessionResult) {
            throw new RenException("解析微信用户信息失败");
        } else if (StringUtils.isBlank(wxMaJscode2SessionResult.getOpenid())) {
            throw new RenException("获取微信openid失败");
        }
        return wxMaJscode2SessionResult;
    }
}
