package com.storlead.sales.controller.customer;

import com.storlead.framework.common.util.ConvertUtils;
import com.storlead.framework.common.result.Result;
import com.storlead.sales.pojo.dto.customer.DataAnalysisDTO;
import com.storlead.sales.pojo.vo.customer.*;
import com.storlead.sales.service.customer.*;
import com.storlead.system.enums.OrganizationalTypeEnum;
import com.storlead.system.service.IUserRoleService;
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.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 javax.annotation.Resource;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 * <p>
 * 汇总列表 前端控制器
 * </p>
 *
 * @author LPK
 * @since 2024-10-22
 */
@RestController
@RequestMapping("/Summary")
@Api(tags = "销售: 汇总管理相关接口")
public class SummaryController {
    @Resource
    private BusinessService businessService;

    @Resource
    private RankingsService rankingsService;

    @Resource
    private IUserService userService;

    @Resource
    private IUserRoleService userRoleService;

    /**
     * 成交商机汇总
     *
     * @param dto
     * @return
     * @throws ParseException
     */
    @PostMapping(value = "/closed-business")
    @ApiOperation(value = "成交商机汇总", notes = "成交商机汇总")
    public Result<?> businessList(@RequestBody DataAnalysisDTO dto) {
        if (!CollectionUtils.isEmpty(dto.getDeptIds()) && CollectionUtils.isEmpty(dto.getOwnerByIds()) ) {
            Set<Long> userIds = userService.getUserIdsByDeptIds(dto.getDeptIds());
            if (!CollectionUtils.isEmpty(userIds)) {
                dto.setOwnerByIds(new ArrayList<>(userIds));
            }
        }
        ClosedBusinessListVO listVO = rankingsService.selectClosedList(dto);
        return Result.result(listVO);
    }

    /**
     * 成交商机汇总
     *
     * @param dto
     * @return
     * @throws ParseException
     */
    @PostMapping(value = "/closed-business-list")
    @ApiOperation(value = "成交商机明细", notes = "成交商机明细")
    public Result<?> closeBusinessList(@RequestBody DataAnalysisDTO dto) {
        if (!CollectionUtils.isEmpty(dto.getDeptIds()) && CollectionUtils.isEmpty(dto.getOwnerByIds()) ) {
            Set<Long> userIds = userService.getUserIdsByDeptIds(dto.getDeptIds());
            if (!CollectionUtils.isEmpty(userIds)) {
                dto.setOwnerByIds(new ArrayList<>(userIds));
            }
        }
        CloseBusinessAllListsVO listVO = rankingsService.selectClosedBusinessList(dto);
        return Result.result(listVO);
    }


    /**
     * 产品销售汇总-按产品分类
     *
     * @param dto
     * @return
     * @throws ParseException
     */
    @PostMapping(value = "/product-sales-prodList")
    @ApiOperation(value = "产品销售汇总-按产品分类", notes = "按产品分类")
    public Result<?> productSalesTotal(@RequestBody DataAnalysisDTO dto) {
        if (!CollectionUtils.isEmpty(dto.getDeptIds()) && CollectionUtils.isEmpty(dto.getOwnerByIds()) ) {
            Set<Long> userIds = userService.getUserIdsByDeptIds(dto.getDeptIds());
            if (!CollectionUtils.isEmpty(userIds)) {
                dto.setOwnerByIds(new ArrayList<>(userIds));
            }
        }
        QuotationSalesTotalVO vo = rankingsService.productSalesTotal(dto);
        return Result.result(vo);
    }

    /**
     * 产品销售汇总-按时间段分类
     *
     * @param dto
     * @return
     * @throws ParseException
     */
    @PostMapping(value = "/product-sales-timeList")
    @ApiOperation(value = "产品销售汇总-按时间段分类", notes = "按时间段分类")
    public Result<?> productSalesTotalByMonth(@RequestBody DataAnalysisDTO dto) {
        if (!CollectionUtils.isEmpty(dto.getDeptIds()) && CollectionUtils.isEmpty(dto.getOwnerByIds()) ) {
            Set<Long> userIds = userService.getUserIdsByDeptIds(dto.getDeptIds());
            if (!CollectionUtils.isEmpty(userIds)) {
                dto.setOwnerByIds(new ArrayList<>(userIds));
            }
        }
        QuotationTimeTotalVO vo = rankingsService.productSalesTotalByMonth(dto);
        return Result.result(vo);
    }

    /**
     * 业务新增排名
     *
     * @param dto
     * @return
     * @throws ParseException
     */
    @PostMapping(value = "/business-rankings")
    @ApiOperation(value = "业务新增排名", notes = "业务新增排名")
    public Result<?> businessRankings(@RequestBody DataAnalysisDTO dto) {
        if (Integer.valueOf(70).equals(dto.getSummaryType())) {
            Set deptScope = userRoleService.dataSelete(OrganizationalTypeEnum.DEPT_GROUP);
            if (!CollectionUtils.isEmpty(dto.getDeptIds())) {
                dto.setDeptIds(ConvertUtils.retainByIds(dto.getDeptIds(),deptScope));
            } else {
                dto.setDeptIds(List.copyOf(deptScope));
            }
            if (!CollectionUtils.isEmpty(dto.getDeptIds())) {
                Set<Long> userIds = userService.getUserIdsByDeptIds(dto.getDeptIds());
                if (CollectionUtils.isEmpty(userIds)) {
                    return Result.result(new SalesDataAddTotalVO());
                }
                dto.setOwnerByIds(List.copyOf(userIds));
            } else {
                return Result.result(new SalesDataAddTotalVO());
            }
        } else {
            if (!CollectionUtils.isEmpty(dto.getDeptIds()) && CollectionUtils.isEmpty(dto.getOwnerByIds()) ) {
                Set<Long> userIds = userService.getUserIdsByDeptIds(dto.getDeptIds());
                if (!CollectionUtils.isEmpty(userIds)) {
                    dto.setOwnerByIds(new ArrayList<>(userIds));
                }
            }

            Set dataScope = userRoleService.dataSelete(OrganizationalTypeEnum.USER_GROUP);
            if (!CollectionUtils.isEmpty(dto.getOwnerByIds())) {
                if (!CollectionUtils.isEmpty(dataScope)) {
                    dto.setOwnerByIds(ConvertUtils.retainByIds(dto.getOwnerByIds(),dataScope));
                }
            } else {
                dto.setOwnerByIds(List.copyOf(dataScope));
            }
        }

        SalesDataAddTotalVO vo = rankingsService.businessRankings(dto);
        return Result.result(vo);
    }

    /**
     * 成交数据排名
     *
     * @param dto
     * @return
     * @throws ParseException
     */
    @PostMapping(value = "/transaction-ranking")
    @ApiOperation(value = "成交数据排名", notes = "成交数据排名")
    public Result<?> rankingTransactionData(@RequestBody DataAnalysisDTO dto) {
        if (Integer.valueOf(70).equals(dto.getSummaryType())) {
            Set deptScope = userRoleService.dataSelete(OrganizationalTypeEnum.DEPT_GROUP);
            if (!CollectionUtils.isEmpty(dto.getDeptIds())) {
                dto.setDeptIds(ConvertUtils.retainByIds(dto.getDeptIds(),deptScope));
            } else {
                dto.setDeptIds(List.copyOf(deptScope));
            }
            if (!CollectionUtils.isEmpty(dto.getDeptIds())) {
                Set<Long> userIds = userService.getUserIdsByDeptIds(dto.getDeptIds());
                if (CollectionUtils.isEmpty(userIds)) {
                    return Result.result(new SalesDataAddTotalVO());
                }
                dto.setOwnerByIds(List.copyOf(userIds));
            } else {
                return Result.result(new SalesDataAddTotalVO());
            }
        } else {
            Set dataScope = userRoleService.dataSelete(OrganizationalTypeEnum.USER_GROUP);
            if (!CollectionUtils.isEmpty(dto.getOwnerByIds())) {
                if (!CollectionUtils.isEmpty(dataScope)) {
                    dto.setOwnerByIds(ConvertUtils.retainByIds(dto.getOwnerByIds(),dataScope));
                }
            } else {
                dto.setOwnerByIds(List.copyOf(dataScope));
            }
        }
        TransactionDataAllVo vo = rankingsService.rankingTransactionData(dto);
        return Result.result(vo);
    }

    /**
     * 转化率排名
     *
     * @param dto
     * @return
     * @throws ParseException
     */
    @PostMapping(value = "/conversion-rate-ranking")
    @ApiOperation(value = "转化率排名", notes = "转化率排名")
    public Result<?> rankingConversionRate(@RequestBody DataAnalysisDTO dto) {
        if (Integer.valueOf(70).equals(dto.getSummaryType())) {
            Set deptScope = userRoleService.dataSelete(OrganizationalTypeEnum.DEPT_GROUP);
            if (!CollectionUtils.isEmpty(dto.getDeptIds())) {
                dto.setDeptIds(ConvertUtils.retainByIds(dto.getDeptIds(),deptScope));
            } else {
                dto.setDeptIds(List.copyOf(deptScope));
            }
            if (!CollectionUtils.isEmpty(dto.getDeptIds())) {
                Set<Long> userIds = userService.getUserIdsByDeptIds(dto.getDeptIds());
                if (CollectionUtils.isEmpty(userIds)) {
                    return Result.result(new SalesDataAddTotalVO());
                }
                dto.setOwnerByIds(List.copyOf(userIds));
            } else {
                return Result.result(new SalesDataAddTotalVO());
            }
        } else {
            Set dataScope = userRoleService.dataSelete(OrganizationalTypeEnum.USER_GROUP);
            if (!CollectionUtils.isEmpty(dto.getOwnerByIds())) {
                if (!CollectionUtils.isEmpty(dataScope)) {
                    dto.setOwnerByIds(ConvertUtils.retainByIds(dto.getOwnerByIds(),dataScope));
                }
            } else {
                dto.setOwnerByIds(List.copyOf(dataScope));
            }
        }
        ConversionRateTotalVo vo = rankingsService.rankingConversionRate(dto);
        return Result.result(vo);
    }

    /**
     * 跟进数据排名
     *
     * @param dto
     * @return
     * @throws ParseException
     */
    @PostMapping(value = "/follow-up-ranking")
    @ApiOperation(value = "跟进数据排名", notes = "跟进数据排名")
    public Result<?> rankingFollowUpData(@RequestBody DataAnalysisDTO dto) {
        if (Integer.valueOf(70).equals(dto.getSummaryType())) {
            Set deptScope = userRoleService.dataSelete(OrganizationalTypeEnum.DEPT_GROUP);
            if (!CollectionUtils.isEmpty(dto.getDeptIds())) {
                dto.setDeptIds(ConvertUtils.retainByIds(dto.getDeptIds(),deptScope));
            } else {
                dto.setDeptIds(List.copyOf(deptScope));
            }
            if (!CollectionUtils.isEmpty(dto.getDeptIds())) {
                Set<Long> userIds = userService.getUserIdsByDeptIds(dto.getDeptIds());
                if (CollectionUtils.isEmpty(userIds)) {
                    return Result.result(new SalesDataAddTotalVO());
                }
                dto.setOwnerByIds(List.copyOf(userIds));
            } else {
                return Result.result(new SalesDataAddTotalVO());
            }
        } else {
            Set dataScope = userRoleService.dataSelete(OrganizationalTypeEnum.USER_GROUP);
            if (!CollectionUtils.isEmpty(dto.getOwnerByIds())) {
                if (!CollectionUtils.isEmpty(dataScope)) {
                    dto.setOwnerByIds(ConvertUtils.retainByIds(dto.getOwnerByIds(),dataScope));
                }
            } else {
                dto.setOwnerByIds(List.copyOf(dataScope));
            }
        }
        UserFollowUpModeTotalVO vo = rankingsService.rankingFollowUpData(dto);
        return Result.result(vo);
    }

}
