package com.jeequan.jeepay.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jeequan.jeepay.db.entity.MchInfo;
import com.jeequan.jeepay.db.entity.PayOrder;
import com.jeequan.jeepay.db.entity.SysUserEntity;
import com.jeequan.jeepay.db.entity.SysUserTeam;
import com.jeequan.jeepay.service.mapper.SysUserTeamMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 人员团队表 服务实现类
 * </p>
 *
 * @author [mybatis plus generator]
 * @since 2022-03-12
 */
@Service
public class SysUserTeamService extends ServiceImpl<SysUserTeamMapper, SysUserTeam> {

    @Autowired private SysUserService sysUserService;
    @Autowired private MchInfoService mchInfoService;
    @Autowired private PayOrderService payOrderService;

    public long queryCount(JSONObject queryObject) {
        return baseMapper.selectUserCount(genQueryParams(queryObject));
    }

    public List<JSONObject> selectList(List<Long> teamIdList, String createdStart, String createdEnd) {
        return baseMapper.selectUserPage(teamIdList, createdStart, createdEnd);
    }

    public Map<String, Object> genQueryParams(JSONObject queryObject) {

        Map<String, Object> params = new HashMap<>();

        if (StringUtils.isNotBlank(queryObject.getString("sysType"))) {
            params.put("sysType", queryObject.getString("sysType"));
        }
        if (StringUtils.isNotBlank(queryObject.getString("belongInfoId"))) {
            params.put("belongInfoId", queryObject.getString("belongInfoId"));
        }
        if (StringUtils.isNotBlank(queryObject.getString("teamName"))) {
            params.put("teamName", queryObject.getString("teamName"));
        }
        if (StringUtils.isNotBlank(queryObject.getString("teamNo"))) {
            params.put("teamNo", queryObject.getString("teamNo"));
        }
        if (StringUtils.isNotBlank(queryObject.getString("telphone"))) {
            params.put("telphone", queryObject.getString("telphone"));
        }
        if (queryObject.getLong("sysUserId") != null) {
            params.put("sysUserId", queryObject.getLong("sysUserId"));
        }

        return params;
    }

    public IPage<SysUserTeam> selectPage(IPage iPage, SysUserTeam sysUserTeam) {

        LambdaQueryWrapper<SysUserTeam> wrapper = new LambdaQueryWrapper<>();
        wrapper.orderByDesc(SysUserTeam::getCreatedAt);
        if (StringUtils.isNotBlank(sysUserTeam.getSysType())) {
            wrapper.eq(SysUserTeam::getSysType, sysUserTeam.getSysType());
        }
        if (StringUtils.isNotBlank(sysUserTeam.getBelongInfoId())) {
            wrapper.eq(SysUserTeam::getBelongInfoId, sysUserTeam.getBelongInfoId());
        }
        if (StringUtils.isNotBlank(sysUserTeam.getTeamName())) {
            wrapper.like(SysUserTeam::getTeamName, sysUserTeam.getTeamName());
        }
        if (StringUtils.isNotBlank(sysUserTeam.getTeamNo())) {
            wrapper.eq(SysUserTeam::getTeamNo, sysUserTeam.getTeamNo());
        }

        return this.page(iPage, wrapper);
    }

    public Map<String, Object> selectTeamData(List<Long> teamIdList) {

        Map<String, Object> result = new HashMap<>();
        result.put("totalMchCount", 0);
        result.put("totalOrderAmount", 0);

        List<SysUserEntity> userList = sysUserService.lambdaQuery()
                .in(SysUserEntity::getTeamId, teamIdList).list();
        if (CollUtil.isEmpty(userList)) {
            return result;
        }

        // 团队下的用户ID集合
        List<Long> userIdList = new LinkedList<>();
        userList.forEach(sysUser -> userIdList.add(sysUser.getSysUserId()));

        List<MchInfo> mchInfoList = mchInfoService.list(MchInfo.gw().in(com.jeequan.jeepay.core.entity.MchInfo::getCreatedUid, userIdList));
        if (CollUtil.isEmpty(userList)) {
            return result;
        }

        // 用户下的商户号集合
        List<String> mchNoList = new LinkedList<>();
        mchInfoList.forEach(mchInfo -> mchNoList.add(mchInfo.getMchNo()));
        result.put("totalMchCount", mchNoList.size());

        // 查询商户订单总金额
        QueryWrapper<PayOrder> gw = new QueryWrapper<>();
        gw.lambda().in(PayOrder::getMchNo, mchNoList);
        gw.select("ifnull(sum(amount), 0) as totalAmount");
        Map<String, Object> map = payOrderService.getMap(gw);
        map.put("totalOrderAmount", map.get("totalAmount"));

        return result;
    }
}
