package com.project.zcustom.service.service.basics.impl;

import com.project.zcustom.domain.basics.PlatAppOrg;
import com.project.zcustom.domain.basics.PlatBasics;
import com.project.zcustom.domain.basics.PlatBasicsCompany;
import com.project.zcustom.mapper.service.basics.PlatBasicsCompanyMapper;
import com.project.zcustom.service.service.basics.IPlatAppOrgService;
import com.project.zcustom.service.service.basics.IPlatBasicsCompanyService;
import com.project.common.core.domain.search.PageParamVo;
import com.project.zcustom.service.service.basics.IPlatBasicsService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
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 com.project.common.utils.StringUtils;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 公司活跃人数Service业务层处理
 *
 * @author change
 * @date 2024-10-21
 */
@Service
public class PlatBasicsCompanyServiceImpl extends ServiceImpl<PlatBasicsCompanyMapper, PlatBasicsCompany> implements IPlatBasicsCompanyService {

    @Resource
    private IPlatAppOrgService iPlatAppOrgService;
    @Resource
    private IPlatBasicsService iPlatBasicsService;

    /**
     * 组装查询条件
     */
    private LambdaQueryWrapper<PlatBasicsCompany> getLambdaQueryWrapper(PlatBasicsCompany platBasicsCompany) {
        LambdaQueryWrapper<PlatBasicsCompany> lqw = new LambdaQueryWrapper<PlatBasicsCompany>();
        if (StringUtils.isNotBlank(platBasicsCompany.getAppOrg())) {
            lqw.eq(PlatBasicsCompany::getAppOrg, platBasicsCompany.getAppOrg());
        }
        if (StringUtils.isNotBlank(platBasicsCompany.getCompanyName())) {
            lqw.like(PlatBasicsCompany::getCompanyName, platBasicsCompany.getCompanyName());
        }
        if (StringUtils.isNotNull(platBasicsCompany.getActiveUserNum())) {
            lqw.eq(PlatBasicsCompany::getActiveUserNum, platBasicsCompany.getActiveUserNum());
        }
        return lqw;
    }

    /**
     * 分页查询公司活跃人数列表
     *
     * @param platBasicsCompany 公司活跃人数
     * @return 公司活跃人数集合
     */
    @Override
    public IPage<PlatBasicsCompany> pagingPlatBasicsCompanyList(PlatBasicsCompany platBasicsCompany) {
        IPage<PlatBasicsCompany> page = new Page<>(platBasicsCompany.getPageNum(), platBasicsCompany.getPageSize());
        LambdaQueryWrapper<PlatBasicsCompany> lqw = getLambdaQueryWrapper(platBasicsCompany);
        // 排序 默认时间倒序
        lqw.orderByDesc(PlatBasicsCompany::getCreateTime);
        return baseMapper.selectPage(page, lqw);
    }

    /**
     * 分页查询公司活跃人数列表
     *
     * @param lqw
     * @return 公司活跃人数集合
     */
    @Override
    public IPage<PlatBasicsCompany> pagingPlatBasicsCompanyList(PageParamVo pageParamVo, LambdaQueryWrapper<PlatBasicsCompany> lqw) {
        IPage<PlatBasicsCompany> page = new Page<>(pageParamVo.getPageNum(), pageParamVo.getPageSize());
        // 排序 默认时间倒序
        lqw.orderByDesc(PlatBasicsCompany::getCreateTime);
        return baseMapper.selectPage(page, lqw);
    }

    /**
     * 查询公司活跃人数列表
     *
     * @param platBasicsCompany 公司活跃人数
     * @return 公司活跃人数
     */
    @Override
    public List<PlatBasicsCompany> selectPlatBasicsCompanyList(PlatBasicsCompany platBasicsCompany) {
        LambdaQueryWrapper<PlatBasicsCompany> lqw = getLambdaQueryWrapper(platBasicsCompany);
        // 排序 默认时间倒序
        lqw.orderByDesc(PlatBasicsCompany::getCreateTime);
        return baseMapper.selectList(lqw);
    }

    /**
     * 查询公司活跃人数列表
     *
     * @param platBasicsCompany 公司活跃人数
     * @return 公司活跃人数
     */
    @Override
    public List<PlatBasicsCompany> selectPlatBasicsCompanyList(PlatBasicsCompany platBasicsCompany, Long limitNum) {
        LambdaQueryWrapper<PlatBasicsCompany> lqw = getLambdaQueryWrapper(platBasicsCompany);
        // 排序 默认时间倒序
        lqw.orderByDesc(PlatBasicsCompany::getActiveUserNum);
        lqw.last("limit " + limitNum);
        return baseMapper.selectList(lqw);
    }

    /**
     * 根据指定字段查询信息
     *
     * @param field      字段名称
     * @param filedValue 字段值
     * @return 公司活跃人数
     */
    @Override
    public PlatBasicsCompany getPlatBasicsCompanyByField(String field, String filedValue) {
        QueryWrapper<PlatBasicsCompany> lqw = new QueryWrapper<PlatBasicsCompany>();
        lqw.eq(field, filedValue);
        lqw.last("limit 1");
        return this.getOne(lqw, false);
    }

    /**
     * 根据指定字段查询信息列表
     *
     * @param field      字段名称
     * @param filedValue 字段值
     * @return 公司活跃人数
     */
    @Override
    public List<PlatBasicsCompany> selectPlatBasicsCompanyListByField(String field, String filedValue, Long limitNum) {
        QueryWrapper<PlatBasicsCompany> lqw = new QueryWrapper<PlatBasicsCompany>();
        lqw.eq(field, filedValue);
        lqw.last("limit " + limitNum);
        lqw.orderByDesc("active_user_num");
        return this.list(lqw);
    }

    /**
     * 根据指定字段查询信息列表
     *
     * @param field      字段名称
     * @param filedValue 字段值
     * @return 公司活跃人数
     */
    @Override
    public List<PlatBasicsCompany> selectPlatBasicsCompanyListByField(String field, String filedValue) {
        QueryWrapper<PlatBasicsCompany> lqw = new QueryWrapper<PlatBasicsCompany>();
        lqw.eq(field, filedValue);
        return this.list(lqw);
    }

    /**
     * 判断指定字段值是否存在
     *
     * @param field      字段名称
     * @param filedValue 字段值
     */
    @Override
    public void checkFieldExist(String field, String filedValue, String tips, Long id) {
        QueryWrapper<PlatBasicsCompany> lqw = new QueryWrapper<PlatBasicsCompany>();
        lqw.eq(field, filedValue);
        lqw.ne("id", id);
        if (this.getOne(lqw, false) != null) {
            throw new RuntimeException(tips + "已存在");
        }
    }

    /**
     * 获取活跃top10公司
     * @param appOrg
     * @return
     */
    @Override
    public Map<String, Object> getTopTenCompany(String appOrg){
        QueryWrapper<PlatBasics> basicsQueryWrapper = new QueryWrapper<>();
        basicsQueryWrapper.eq("CHAR_LENGTH(app_org)",5);
        basicsQueryWrapper.select("*,CASE" +
                " WHEN order_num = 0 OR register_num = 0 THEN 0" +
                " ELSE ROUND((order_num / register_num) * 100, 2)" +
                " END AS active_rate");
        basicsQueryWrapper.last("limit 10");
        basicsQueryWrapper.orderByDesc("active_rate");
        basicsQueryWrapper.orderByAsc("app_org");
        List<PlatBasics> list = iPlatBasicsService.list(basicsQueryWrapper);
        Map<String, Object> tempMap = new HashMap<>();
        List<String> companyNameList;
        List<BigDecimal> activeUserNumList;
        if (list.isEmpty()){
            QueryWrapper<PlatAppOrg> orgQueryWrapper = new QueryWrapper<>();
            orgQueryWrapper.eq("CHAR_LENGTH(app_org)",5);
            orgQueryWrapper.last("limit 10");
            orgQueryWrapper.orderByAsc("app_org");
            List<PlatAppOrg> appOrgList = iPlatAppOrgService.list(orgQueryWrapper);
            companyNameList = appOrgList.stream().map(PlatAppOrg::getAppName).collect(Collectors.toList());
            activeUserNumList = Collections.nCopies(10,BigDecimal.ZERO);
        }else{
            //  查询单位名称
            List<String> codeList = list.stream().map(PlatBasics::getAppOrg).collect(Collectors.toList());
            companyNameList = codeList.stream().map(code -> iPlatAppOrgService.getPlatAppOrgByField("app_org",code).getAppName()).collect(Collectors.toList());
            activeUserNumList = list.stream().map(PlatBasics::getActiveRate).collect(Collectors.toList());
        }
        StringUtils.createdTableData(companyNameList,activeUserNumList,"#2EC7C9",tempMap);
        return tempMap;
    }
}
