package com.storlead.sales.controller.target;


import com.storlead.framework.common.result.Result;
import com.storlead.sales.pojo.dto.target.TargetQuotaSearchDTO;
import com.storlead.sales.pojo.dto.target.TargetRankingDTO;
import com.storlead.sales.service.target.TargetQuotaService;
import com.storlead.sales.service.target.TargetRinkService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import javax.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 目标配额表 前端控制器
 * </p>
 *
 * @author YPZ
 * @since 2024-12-11
 */
@RestController
@RequestMapping("/target-quota")
@Api(tags = "销售: 业绩目标管理相关接口")
public class TargetQuotaController {

    @Resource
    private TargetQuotaService targetQuotaService;

    @Resource
    private TargetRinkService targetRinkService;

    @PostMapping(value = "/amount-rink")
    @ApiOperation(value = "首页-成交金额排行", notes = "成交金额排行")
    public Result<?>  getOrderlAmount(@RequestBody TargetRankingDTO targetRankingDTO) {
        int currentYear = LocalDate.now().getYear();
        targetRankingDTO.setYear(currentYear);
        targetRankingDTO.setOrganizationalType(4);
        targetRankingDTO.setTotalOrValue(1);
        Map<String, Object> orderAmount = targetQuotaService.getTargetOrder(targetRankingDTO);
        return  Result.result(orderAmount);
    }


    @PostMapping(value = "/target-Rink")
    @ApiOperation(value = "业绩目标排行", notes = "业绩目标排行")
    public Result<?>  getTargetRink(@RequestBody TargetRankingDTO targetRankingDTO) {
        Map<String, Object> allResults = new HashMap<>();

        allResults.put("orderAmount", targetQuotaService.getTargetOrder(targetRankingDTO));
        allResults.put("orderCount", targetQuotaService.getTargetOrderCount(targetRankingDTO));
        allResults.put("customers", targetQuotaService.getTargetCustomer(targetRankingDTO));
        allResults.put("businessCount", targetQuotaService.getBusinessCount(targetRankingDTO));
        allResults.put("followUpCount", targetQuotaService.getFollowUpCount(targetRankingDTO));
        allResults.put("newClue", targetQuotaService.getNewClue(targetRankingDTO));

        return Result.result(allResults);
    }

    @PostMapping(value = "/all-target-Rink")
    @ApiOperation(value = "业绩目标排行-合计排行", notes = "合计排行")
    public Result<?>  getAllTargetRink(@RequestBody TargetRankingDTO targetRankingDTO) {
        return Result.result(targetQuotaService.getOverallRanking(targetRankingDTO));
    }



    @PostMapping(value = "/progress-list")
    @ApiOperation(value = "业绩目标进度", notes = "业绩目标进度")
    public Result<?> getTotalAmount(@RequestBody TargetRankingDTO targetRankingDTO) {
        try {
            switch (targetRankingDTO.getType()) {
                case 41:
                    return Result.result(targetRinkService.getTotalAmount(targetRankingDTO));
                case 40:
                    return Result.result(targetRinkService.getTotalOrders(targetRankingDTO));
                case 10:
                    return Result.result(targetRinkService.getNewCustomers(targetRankingDTO));
                case 30:
                    return Result.result(targetRinkService.getNewBusinesses(targetRankingDTO));
                case 11:
                    return Result.result(targetRinkService.getNewClues(targetRankingDTO));
                case 12:
                    return Result.result(targetRinkService.getFollowUpCounts(targetRankingDTO));
                default:
                    throw new IllegalArgumentException("无效的 type 值: " + targetRankingDTO.getType());
            }
        } catch (IllegalArgumentException e) {
            return Result.error(e.getMessage());
        }
    }

    @PostMapping(value = "/total-count")
    @ApiOperation(value = "合计进度", notes = "合计进度")
    public Result<?> getTotalProgress(@RequestBody TargetRankingDTO targetRankingDTO){
        Map<String, Object> totalProgress = targetRinkService.getTotalProgress(targetRankingDTO);
        return Result.result(totalProgress);
    }

    @PostMapping(value = "/uesr-all-progress")
    @ApiOperation(value = "首页-用户总业绩目标进度", notes = "用户总业绩目标进度")
    public Result<?> getUserAllProgress(@RequestBody TargetRankingDTO targetRankingDTO){
        return Result.result(targetRinkService.getUserTotalProgress(targetRankingDTO));
    }

    @PostMapping(value = "/uesr-progress")
    @ApiOperation(value = "首页-用户业绩目标进度", notes = "用户业绩目标进度")
    public Result<?> getUserProgress(@RequestBody TargetRankingDTO targetRankingDTO){
        return Result.result(targetRinkService.getCurrentMonthProgress(targetRankingDTO));
    }

//    @PostMapping(value = "/edit")
//    @ApiOperation(value = "业绩目标管理-修改方案配置", notes = "业绩目标管理-修改方案配置")
//    public Result<?> edit(@RequestBody TargetPlanAddDTO targetPlanAddDTO) {
//        return targetPlanService.editPlan(targetPlanAddDTO);
//    }

    @PostMapping(value = "/list")
    @ApiOperation(value = "业绩目标管理-目标列表", notes = "业绩目标管理-目标列表")
    public Result<?> list(@RequestBody TargetQuotaSearchDTO targetQuotaSearchDTO) {
        return targetQuotaService.selectListVO(targetQuotaSearchDTO);
    }

    @GetMapping(value = "/get-by-id")
    @ApiOperation(value = "业绩目标管理-通过id查看汇总详情", notes = "业绩目标管理-通过id查看汇总详情")
    public Result<?> getById(@RequestParam(name="id",required=false) Long id) {

        return targetQuotaService.getTargetQuotaVOById(id);
    }

}
