package com.gic.haoban.manage.web.controller.content;

import cn.hutool.core.date.DateUtil;
import com.gic.api.base.commons.BasePageInfo;
import com.gic.api.base.commons.Page;
import com.gic.api.base.commons.ServiceResponse;
import com.gic.commons.webapi.reponse.RestResponse;
import com.gic.content.api.dto.material.ContentMaterialFrontDTO;
import com.gic.content.api.qdto.material.ContentMaterialPageFrontQDTO;
import com.gic.content.api.service.ContentMaterialApiService;
import com.gic.haoban.manage.web.controller.content.adaptor.ClerkStoreAdaptor;
import com.gic.haoban.manage.web.controller.content.adaptor.MaterialDataAdaptor;
import com.gic.haoban.manage.web.qo.content.ContentMaterialBaseQO;
import com.gic.haoban.manage.web.qo.content.statistics.MaterialAnalyzeDataQO;
import com.gic.haoban.manage.web.qo.content.statistics.MaterialPersonalUsedQO;
import com.gic.haoban.manage.web.qo.content.statistics.MaterialStatisticsBaseQO;
import com.gic.haoban.manage.web.vo.content.statistics.MaterialClerkUsedDataVO;
import com.gic.haoban.manage.web.vo.content.statistics.MaterialDataOverviewVO;
import com.gic.haoban.manage.web.vo.content.statistics.MaterialIndexDataVO;
import com.gic.haoban.manage.web.vo.content.statistics.MaterialRealDataVO;
import com.gic.haoban.manage.web.vo.content.statistics.MaterialStoreUsedDataVO;
import com.gic.haoban.manage.web.vo.content.statistics.report.MaterialPersonalUsedDataVO;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Optional;

/**
 * 素材-好办数据统计
 **/
@RequestMapping(path = "/material/data")
@RestController
public class MaterialDataStaticsController {

    @Autowired
    private ClerkStoreAdaptor clerkStoreAdaptor;
    @Autowired
    private MaterialDataAdaptor materialDataAdaptor;
    @Autowired
    private ContentMaterialApiService contentMaterialApiService;

    /**
     * 获取素材首页数据
     *
     * @param contentMaterialBaseQO
     * @return
     */
    @RequestMapping(path = "/content/material/index/data")
    public RestResponse<MaterialIndexDataVO> queryMaterialIndexData(@RequestBody ContentMaterialBaseQO contentMaterialBaseQO) {

        ContentMaterialPageFrontQDTO search = new ContentMaterialPageFrontQDTO();
        search.setEnterpriseId(contentMaterialBaseQO.getEnterpriseId());
        search.setPublishBeginStartTime(DateUtil.beginOfDay(new Date()));
        search.setPublishBeginEndTime(DateUtil.endOfDay(new Date()));
        if (StringUtils.isBlank(contentMaterialBaseQO.getStoreId())) {
            // 区经 获取管辖的权限
            List<String> storeIds = this.clerkStoreAdaptor.queryClerkStoreIds(contentMaterialBaseQO.getClerkId(), contentMaterialBaseQO.getWxEnterpriseId());
            search.setStoreIdList(storeIds);
        } else {
            search.setStoreIdList(Collections.singletonList(contentMaterialBaseQO.getStoreId()));
        }
        BasePageInfo pageInfo = new BasePageInfo();
        pageInfo.setPageNum(1);
        pageInfo.setPageSize(1);
        int totalMaterialNum = 0;
        ServiceResponse<Page<ContentMaterialFrontDTO>> serviceResponse = this.contentMaterialApiService.pageMaterialFront(search, pageInfo);
        if (serviceResponse.isSuccess() && serviceResponse.getResult() != null) {
            totalMaterialNum = serviceResponse.getResult().getTotalCount();
        }
        int totalManagerStoreNum = 0;
        List<String> storeIds = clerkStoreAdaptor.queryClerkStoreIds(contentMaterialBaseQO.getClerkId(), contentMaterialBaseQO.getWxEnterpriseId());
        MaterialIndexDataVO temp = materialDataAdaptor.queryMaterialIndexData(contentMaterialBaseQO.getEnterpriseId(), storeIds);
        if (CollectionUtils.isEmpty(storeIds)) {
            // 超管
            totalMaterialNum = clerkStoreAdaptor.countAdminManagerStore(contentMaterialBaseQO.getClerkId(), contentMaterialBaseQO.getWxEnterpriseId());
        }
        MaterialIndexDataVO indexDataVO = MaterialIndexDataVO.builder()
                .newMaterialNum(totalMaterialNum)
                .useMaterialClerkNum(temp.getUseMaterialClerkNum())
                .unUseMaterialClerkNum(temp.getUnUseMaterialClerkNum())
                .conversionAmount(Optional.ofNullable(temp.getConversionAmount()).orElse(BigDecimal.ZERO))
                .storeManagerNum(totalManagerStoreNum)
                .build();
        return RestResponse.successResult(indexDataVO);
    }


    /**
     * 素材使用分析页面-实时数据统计
     *
     * @return
     */
    @RequestMapping(path = "/content/material/real/use/data")
    public RestResponse<MaterialRealDataVO> queryMaterialUsedAnalyze(@RequestBody MaterialStatisticsBaseQO materialStatisticsBaseQO) {


        return RestResponse.successResult();
    }

    /**
     * 素材使用分析页面-数据概览
     *
     * @param materialStatisticsBaseQO
     * @return
     */
    @RequestMapping(path = "/content/material/overview/data")
    public RestResponse<MaterialDataOverviewVO> queryMaterialOverviewData(@RequestBody MaterialAnalyzeDataQO materialStatisticsBaseQO) {
        return RestResponse.successResult();
    }


    /**
     * 素材使用分析页面-导购纬度使用数据
     *
     * @return
     */
    @RequestMapping(path = "/content/material/clerk/data")
    public RestResponse<Page<MaterialClerkUsedDataVO>> queryMaterialClerkUsedData(@RequestBody MaterialAnalyzeDataQO materialStatisticsBaseQO) {
        //
        return RestResponse.successResult();
    }

    /**
     * 素材使用分析页面-门店纬度使用数据
     *
     * @return
     */
    @RequestMapping(path = "/content/material/store/data")
    public RestResponse<Page<MaterialStoreUsedDataVO>> queryMaterialStoreUsedData(@RequestBody MaterialAnalyzeDataQO materialStatisticsBaseQO) {
        return RestResponse.successResult();
    }


    // 导购 周报/月报数据

    /**
     * 周/月报--导购/店长-素材使用个人数据
     *
     * @return
     */
    @RequestMapping(path = "/content/material/personal/used/data")
    public RestResponse<MaterialPersonalUsedDataVO> queryMaterialUsedReport(@RequestBody MaterialPersonalUsedQO materialPersonalUsedQO) {
        return RestResponse.successResult();
    }

    /**
     * 周/月报--查询导购素材使用数据列表
     *
     * @return
     */
    @RequestMapping(path = "/content/material/personal/used/data/list")
    public RestResponse<Page<MaterialPersonalUsedDataVO>> queryMaterialUsedReportList(@RequestBody MaterialPersonalUsedQO materialPersonalUsedQO) {
        return RestResponse.successResult();
    }

    /**
     * 周/月报--查询门店纬度素材使用数据(店长和区经)
     * @param materialPersonalUsedQO
     * @return
     */
    @RequestMapping(path = "/content/material/store/used/data")
    public RestResponse<MaterialStoreUsedDataVO> queryMaterialStoreUseReport(@RequestBody MaterialPersonalUsedQO materialPersonalUsedQO) {
        return RestResponse.successResult();
    }

    /**
     * 周/月报--查询门店纬度素材使用数据列表
     * @param materialPersonalUsedQO
     * @return
     */
    @RequestMapping(path = "/content/material/store/used/data/list")
    public RestResponse<Page<MaterialStoreUsedDataVO>> queryMaterialStoreUseReportList(@RequestBody MaterialPersonalUsedQO materialPersonalUsedQO) {
        return RestResponse.successResult();
    }

}
