package com.hzs.retail.member.controller.api;


import com.hzs.common.core.web.controller.BaseController;
import com.hzs.common.core.web.domain.AjaxResult;
import com.hzs.common.core.web.page.TableDataInfo;
import com.hzs.common.security.utils.SecurityUtils;
import com.hzs.retail.member.param.MarketDynamicsQueryDateParam;
import com.hzs.retail.member.service.ICuMemberRetailAchieveService;
import com.hzs.retail.member.vo.CuMemberRetailAchieveVO;
import com.hzs.retail.member.vo.CuMemberRetailDataStatisticsResultVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 会员首页
 */
@Slf4j
@RequestMapping("/api/retail-member")
@RestController
public class ApiRetailHomePageController extends BaseController {

    @Autowired
    private ICuMemberRetailAchieveService retailAchieveService;

    /**
     * 查询会员盒数
     *
     * @return AjaxResult
     */
    @GetMapping("/member-box")
    public AjaxResult selectMemberBox() {
        Long pkMember = SecurityUtils.getUserId();
        Integer pkCountry = SecurityUtils.getPkCountry();
        Integer systemType = SecurityUtils.getSystemType();
        CuMemberRetailDataStatisticsResultVO result = retailAchieveService.getRetailDataStatisticsDetail(pkMember, pkCountry, systemType);
        CuMemberRetailAchieveVO vo = CuMemberRetailAchieveVO.builder()
                .totalBox(result.getTotalBoxNum())
                .bigAreaBox(result.getBigRangeTotalBoxNum())
                .smallAreaBox(result.getSmallRangeTotalBoxNum())
                .totalSumPv(result.getTotalPv())
                .bigArealPv(result.getBigRangeTotalPv())
                .smallAreaPv(result.getSmallRangeTotalPv())
                .build();
        return AjaxResult.success(vo);
    }

    /**
     * 查询会员业绩
     *
     * @return AjaxResult
     */
    @GetMapping("/member-achieve")
    public AjaxResult selectMemberAchieve() {
        Long pkMember = SecurityUtils.getUserId();
        Integer pkCountry = SecurityUtils.getPkCountry();
        Integer systemType = SecurityUtils.getSystemType();
        CuMemberRetailAchieveVO vo = retailAchieveService.selectMemberAchieve(pkMember, pkCountry, systemType);
        return AjaxResult.success(vo);
    }


    /**
     * 查询会员业绩
     *
     * @return AjaxResult
     */
    @GetMapping("/member-big-achieve")
    public AjaxResult selectMemberBigAchieve() {
        Long pkMember = SecurityUtils.getUserId();
        Integer pkCountry = SecurityUtils.getPkCountry();
        Integer systemType = SecurityUtils.getSystemType();
        return AjaxResult.success(retailAchieveService.selectMemberBigAchieve(pkMember, pkCountry, systemType));
    }

    /**
     * 查询会员业绩列表
     *
     * @return AjaxResult
     */
    @PostMapping("/member-achieve-list")
    public TableDataInfo selectMemberAchieveList(@RequestBody CuMemberRetailAchieveVO cuMemberRetailAchieveVO) {
        Long pkMember = SecurityUtils.getUserId();
        cuMemberRetailAchieveVO.setPkMember(pkMember);
        Integer systemType = SecurityUtils.getSystemType();
        cuMemberRetailAchieveVO.setSystemType(systemType);
        cuMemberRetailAchieveVO.setPkCountry(SecurityUtils.getPkCountry());
        cuMemberRetailAchieveVO.setPkMemberList(retailAchieveService.selectDirectPushByPkMember(pkMember, systemType));
        startPage();
        List<CuMemberRetailAchieveVO> retailAchieveVOList = retailAchieveService.selectMemberAchieveDetail(cuMemberRetailAchieveVO);
        return getDataTable(retailAchieveVOList);
    }


    /**
     * 查询会员业绩
     *
     * @return AjaxResult
     */
    @GetMapping("/member-small-achieve")
    public AjaxResult selectMemberSmallAchieve() {
        Long pkMember = SecurityUtils.getUserId();
        Integer pkCountry = SecurityUtils.getPkCountry();
        Integer systemType = SecurityUtils.getSystemType();
        return AjaxResult.success(retailAchieveService.selectExcludeWaterSmallAchieve(pkMember, pkCountry, systemType));
    }

    /**
     * 查询市场动态（大小区详情）
     *
     * @return AjaxResult
     */
    @GetMapping("/member-achieve-detail")
    public AjaxResult selectMemberBigSmallAchieve() {
        Long pkMember = SecurityUtils.getUserId();
        Integer pkCountry = SecurityUtils.getPkCountry();
        Integer systemType = SecurityUtils.getSystemType();
        return AjaxResult.success(retailAchieveService.selectMemberBigSmallAchieveDetail(pkMember, pkCountry, systemType));
    }


    /**
     * 查询会员大小区盒数（市场动态）
     *
     * @return AjaxResult
     */
    @GetMapping("/big-small-box")
    public AjaxResult selectMemberBigSmallBox(Long pkBigMember) {
        CuMemberRetailAchieveVO param = CuMemberRetailAchieveVO.builder()
                .pkMember(SecurityUtils.getUserId()).pkCountry(SecurityUtils.getPkCountry()).systemType(SecurityUtils.getSystemType())
                .pkBigMember(pkBigMember).build();
        return AjaxResult.success(retailAchieveService.selectMemberBigSmallBox(param));
    }


    /**
     * 查询大区盒数列表（市场动态）
     *
     * @return AjaxResult
     */
    @GetMapping("/big-box-list")
    public AjaxResult selectMemberBigBoxList(Long pkBigMember) {
        CuMemberRetailAchieveVO param = CuMemberRetailAchieveVO.builder()
                .pkMember(SecurityUtils.getUserId()).pkCountry(SecurityUtils.getPkCountry()).systemType(SecurityUtils.getSystemType())
                .pkBigMember(pkBigMember).build();
        return AjaxResult.success(retailAchieveService.selectMemberBigBoxList(param));
    }

    /**
     * 查询会员小区盒数列表（市场动态）
     *
     * @return AjaxResult
     */
    @PostMapping("/small-box-list")
    public TableDataInfo selectMemberSmallBoxList(@RequestBody CuMemberRetailAchieveVO cuMemberRetailAchieveVO) {
        Long pkMember = SecurityUtils.getUserId();
        cuMemberRetailAchieveVO.setPkMember(pkMember);
        Integer systemType = SecurityUtils.getSystemType();
        cuMemberRetailAchieveVO.setSystemType(systemType);
        cuMemberRetailAchieveVO.setPkCountry(SecurityUtils.getPkCountry());
        cuMemberRetailAchieveVO.setPkMemberList(retailAchieveService.selectDirectPushByPkMember(pkMember, systemType));
        startPage();
        List<CuMemberRetailAchieveVO> retailAchieveVOList = retailAchieveService.selectMemberSmallBoxList(cuMemberRetailAchieveVO);
        return getDataTable(retailAchieveVOList);
    }

    /**
     * 市场动态
     */
    @PostMapping("/market-dynamics")
    public AjaxResult getMarketDynamics(@RequestBody MarketDynamicsQueryDateParam param) {
        return AjaxResult.success(retailAchieveService.getMarketDynamics(param));
    }
}
