package cn.com.ctop.kuaishou.modules.report.service.impl;

import cn.com.ctop.common.module.utils.Check;
import cn.com.ctop.kuaishou.modules.report.entity.KuaishouAudienceReportDailyAccount;
import cn.com.ctop.kuaishou.modules.report.mapper.KuaishouAudienceReportDailyAccountMapper;
import cn.com.ctop.kuaishou.modules.report.service.IKuaishouAudienceReportDailyAccountService;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 人群分析报表
 *
 * @author jeecg-boot
 * @version V1.0
 * @date 2020-12-03
 */
@Slf4j
@Service
public class KuaishouAudienceReportDailyAccountServiceImpl extends ServiceImpl<KuaishouAudienceReportDailyAccountMapper, KuaishouAudienceReportDailyAccount> implements IKuaishouAudienceReportDailyAccountService {

    @Autowired
    private KuaishouAudienceReportDailyAccountMapper accountMapper;


    @Override
    public PageInfo<JSONObject> queryPageLists(KuaishouAudienceReportDailyAccount audienceReport, Integer pageNo, Integer pageSize) {
        if (null == pageNo || pageNo == 0) {
            pageNo = 1;
        }
        if (null == pageSize || pageSize == 0) {
            pageSize = 10;
        }
        PageHelper.startPage(pageNo, pageSize);
        List<JSONObject> jsonObjects = accountMapper.queryPageLists(audienceReport);
        return new PageInfo<>(jsonObjects);
    }

    /**
     * 首页报表数据
     *
     * @param type province: 省级地域分布, city: 地级地域分布,gender: 性别分布,ageSegment: 年龄分布clientId: 系统分布 businessInterestTags: 商业兴趣分布
     * @return org.jeecg.common.api.vo.Result<java.lang.Object>
     * @throws
     * @author ZHAOXA
     */
    @Override
    public Result<Object> queryAllReportForm(KuaishouAudienceReportDailyAccount audienceReport) {
        JSONObject data = new JSONObject();
        audienceReport.setAudienceType("province");
        List<JSONObject> provinces = accountMapper.queryAllReportForm(audienceReport);
        if (!Check.isNull(provinces)) {
            data.put("province", provinces);
        }
        audienceReport.setAudienceType("gender");
        List<JSONObject> genders = accountMapper.queryAllReportForm(audienceReport);
        if (!Check.isNull(genders)) {
            data.put("gender", genders);
        }
        audienceReport.setAudienceType("ageSegment");
        List<JSONObject> ages = accountMapper.queryAllReportForm(audienceReport);
        if (!Check.isNull(ages)) {
            data.put("ageSegment", ages);
        }
        audienceReport.setAudienceType("clientId");
        List<JSONObject> clients = accountMapper.queryAllReportForm(audienceReport);
        if (!Check.isNull(clients)) {
            data.put("client", clients);
        }
        audienceReport.setAudienceType("businessInterestTags");
        List<String> busTypes = accountMapper.querybusinessTypeCount(audienceReport);
        audienceReport.setBusTypes(busTypes);
        List<JSONObject> bussiness = accountMapper.querybusinessTypeReportForm(audienceReport);
        if (!Check.isNull(bussiness)) {
            data.put("businessInterestTags", bussiness);
        }
        if (data.size() == 0) {
            return Result.ok("未查询到数据");
        }
        return Result.ok(data);
    }


}
