package com.storlead.sales.controller.report;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.storlead.framework.common.constant.CommonConstant;
import com.storlead.framework.util.LoginUserUtil;
import com.storlead.framework.common.result.Result;
import com.storlead.sales.pojo.dto.report.CustomerStatusAnalysisDTO;
import com.storlead.sales.pojo.dto.report.DataSummaryDTO;
import com.storlead.sales.pojo.vo.report.CustomerStatusTotalVO;
import com.storlead.sales.pojo.vo.report.DataSummaryTotalVO;
import com.storlead.sales.service.CustomerAttributeAnalysisService;
import com.storlead.user.pojo.entity.UserEntity;
import com.storlead.user.service.IUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import javax.annotation.Resource;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/***
  * @Description: 客户属性分析api
  * @Param:
  * @return:
  * @Author: YPZ
  * @Date: 2024/12/27 16:44
  */
@RestController
@RequestMapping("/customer/attribute")
@Api(tags = "报表: 客户属性统计")
public class CustomerAttributeAnalysisApiController {

    @Resource
    private CustomerAttributeAnalysisService customerAttributesService;
    @Resource
    private IUserService userService;
    @PostMapping(value = "/customer-status-analytics-total")
    @ApiOperation(value = "客户属性分析-客户状态分析汇总")
    public Result<?> customerStatusAnalyticsTotal(@RequestBody CustomerStatusAnalysisDTO customerStatusAnalysisDTO) {

        List<CustomerStatusTotalVO> customerStatusTotalVOList = customerAttributesService.customerStatusAnalyticsTotal(customerStatusAnalysisDTO);
        int total = 0;
        if (!CollectionUtils.isEmpty(customerStatusTotalVOList)){
            total = customerStatusTotalVOList.stream().mapToInt(CustomerStatusTotalVO::getAddCustomerNum).sum();
        }

        Map<String,Object> map = new HashMap<>();
        map.put("number",total);
        map.put("list",customerStatusTotalVOList);
        return Result.result(map);
    }

    @PostMapping(value = "/customer-source-analytics-total")
    @ApiOperation(value = "客户属性分析-客户来源分析汇总")
    public Result<?> customerSourceAnalyticsTotal(@RequestBody CustomerStatusAnalysisDTO customerStatusAnalysisDTO) {

        List<CustomerStatusTotalVO> customerSourceTotalVOList = customerAttributesService.customerSourceAnalyticsTotal(customerStatusAnalysisDTO);
        int total = 0;
        if (!CollectionUtils.isEmpty(customerSourceTotalVOList)){
            total = customerSourceTotalVOList.stream().mapToInt(CustomerStatusTotalVO::getAddCustomerNum).sum();
        }

        Map<String,Object> map = new HashMap<>();
        map.put("number",total);
        map.put("list",customerSourceTotalVOList);
        return Result.result(map);
    }

    @PostMapping(value = "/customer-level-analytics-total")
    @ApiOperation(value = "客户属性分析-客户等级分析汇总")
    public Result<?> customerLevelAnalyticsTotal(@RequestBody CustomerStatusAnalysisDTO customerStatusAnalysisDTO) {

        List<CustomerStatusTotalVO> customerStatusTotalVOList = customerAttributesService.customerLevelAnalyticsTotal(customerStatusAnalysisDTO);
        int total = 0;
        if (!CollectionUtils.isEmpty(customerStatusTotalVOList)){
            total = customerStatusTotalVOList.stream().mapToInt(CustomerStatusTotalVO::getAddCustomerNum).sum();
        }

        Map<String,Object> map = new HashMap<>();
        map.put("number",total);
        map.put("list",customerStatusTotalVOList);
        return Result.result(map);
    }

    @PostMapping(value = "/customer-country-analytics-total")
    @ApiOperation(value = "客户属性分析-客户区域分析汇总")
    public Result<?> customerCountryAnalyticsTotal(@RequestBody CustomerStatusAnalysisDTO customerStatusAnalysisDTO) {

        List<CustomerStatusTotalVO> customerStatusTotalVOList = customerAttributesService.customerCountryAnalyticsTotal(customerStatusAnalysisDTO);
        int total = 0;
        if (!CollectionUtils.isEmpty(customerStatusTotalVOList)){
            total = customerStatusTotalVOList.stream().mapToInt(CustomerStatusTotalVO::getAddCustomerNum).sum();
        }

        Map<String,Object> map = new HashMap<>();
        map.put("number",total);
        map.put("list",customerStatusTotalVOList);
        return Result.result(map);
    }

    @PostMapping(value = "/data-summary")
    @ApiOperation(value = "首页-数据简报")
    public Result<?> dataSummary(@RequestBody DataSummaryDTO dataSummaryDTO) {
        List<Long> userIdList = new ArrayList<>();
        if (ObjectUtils.isEmpty(dataSummaryDTO.getOrganizationalType())){
            dataSummaryDTO.setOrganizationalType(4);
        }
        if (ObjectUtils.isEmpty(dataSummaryDTO.getOrganizationalId())){
            userIdList.add(LoginUserUtil.getCurrentUserId());
        }else{
            if (dataSummaryDTO.getOrganizationalType() == 4){
                userIdList.add(dataSummaryDTO.getOrganizationalId());
            }
            LambdaQueryWrapper<UserEntity> userWrapper = new LambdaQueryWrapper<>();
            userWrapper.eq(UserEntity::getIsDelete, CommonConstant.DEL_FLAG_0);
            userWrapper.ne(UserEntity::getStatus, 5);

            if (dataSummaryDTO.getOrganizationalType() == 3){
                userWrapper.in(UserEntity::getDeptId, dataSummaryDTO.getOrganizationalId());
            }
            if (dataSummaryDTO.getOrganizationalType() == 2){
                userWrapper.in(UserEntity::getSubCompanyId, dataSummaryDTO.getOrganizationalId());
            }
            List<UserEntity> userList = userService.list(userWrapper);
            if (ObjectUtils.isEmpty(userList)){
                return Result.result(new DataSummaryTotalVO());
            }
            userIdList = userList.stream().map(UserEntity::getId).collect(Collectors.toList());
        }
        dataSummaryDTO.setUserIdList(userIdList);

        return customerAttributesService.selectDataSummary(dataSummaryDTO);
    }

    @PostMapping(value = "/new-data")
    @ApiOperation(value = "首页-月成交/新增业务数据汇总")
    public Result<?> homeDataSummary(@RequestBody DataSummaryDTO dataSummaryDTO) {
        return customerAttributesService.selectHomeDataSummary(dataSummaryDTO);
    }


}
