package com.ruoyi.ibs.cmpm.controller;

import com.github.pagehelper.Page;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataPageInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.ibs.cmpm.domain.dto.CustLevelDTO;
import com.ruoyi.ibs.cmpm.domain.dto.CustManagerDTO;
import com.ruoyi.ibs.cmpm.domain.dto.GridCmpmListDTO;
import com.ruoyi.ibs.cmpm.domain.dto.UserDeptDTO;
import com.ruoyi.ibs.cmpm.domain.vo.DwbRetailCustLevelManagerDetailVO;
import com.ruoyi.ibs.cmpm.domain.vo.DwbRetailResultVO;
import com.ruoyi.ibs.cmpm.domain.vo.GridCmpmVO;
import com.ruoyi.ibs.cmpm.service.GridCmpmCustService;
import com.ruoyi.ibs.cmpm.service.GridCmpmService;
import com.ruoyi.ibs.cmpm.service.GridCmpmTransferService;
import com.ruoyi.ibs.customerselect.domain.CustBaseInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * @Author 吴凯程
 * @Date 2025/10/20
 **/
@Api(tags = "绩效网格")
@RestController
@RequestMapping("/grid/cmpm")
public class GridCmpmController extends BaseController {

    @Resource
    private GridCmpmService gridCmpmService;

    @Resource
    private GridCmpmCustService gridCmpmCustService;

    @Resource
    private GridCmpmTransferService gridCmpmTransferService;

    @GetMapping("/list")
    @Log(title = "绩效网格-查询绩效网格列表")
    @ApiOperation("查询绩效网格列表")
    public TableDataPageInfo selectManageList(GridCmpmListDTO gridCmpmListDTO) {
        Page<Object> page = startPage();
        List<GridCmpmVO> gridCmpmVOS = gridCmpmService.selectManageList(gridCmpmListDTO);
        return getDataTable(gridCmpmVOS, page);
    }

    @GetMapping("/common/list")
    @Log(title = "绩效网格-查询绩效网格公共池列表")
    @ApiOperation("查询绩效网格公共池列表")
    public TableDataPageInfo selectCommonList(GridCmpmListDTO gridCmpmListDTO) {
        UserDeptDTO userDeptDTO = gridCmpmTransferService.getUserDeptDTO(SecurityUtils.getUsername());
        if (Objects.nonNull(userDeptDTO.getBranchId())){
            gridCmpmListDTO.setBranchId(userDeptDTO.getBranchId());
        }
        Page<Object> page = startPage();
        List<GridCmpmVO> gridCmpmVOS = gridCmpmService.selectCommonList(gridCmpmListDTO);
        return getDataTable(gridCmpmVOS, page);
    }

    @GetMapping("/cust/level")
    @Log(title = "绩效网格-查询客户分层等级")
    @ApiOperation("查询客户分层等级")
    public AjaxResult custLevel(CustLevelDTO custLevelDTO) {
        return success(gridCmpmService.getCustLevelByCust(custLevelDTO));
    }

    @GetMapping("/cust/list/tag")
    @Log(title = "绩效网格-查询客户分层等级下拉框")
    @ApiOperation("查询客户分层等级下拉框")
    @ApiImplicitParam(name = "gridType", value = "零售retail 对公corporate 对公账户corporate_account", required = true, dataType = "String")
    public AjaxResult custLevelListTag(@RequestParam String gridType) {
        return success(gridCmpmService.getAllCustLevel(gridType));
    }

    @GetMapping("/custManagerList")
    @Log(title = "绩效网格-查询管户报表")
    @ApiOperation("查询管户报表")
    public TableDataPageInfo selectCustManagerList(CustManagerDTO custManagerDTO) {
        Page<Object> page = startPage();
        List<DwbRetailCustLevelManagerDetailVO> gridCmpmVOS = gridCmpmService.selectCustManagerList(custManagerDTO);
        return getDataTable(gridCmpmVOS, page);
    }

    @GetMapping("/custManager/result")
    @Log(title = "绩效网格-管户报表统计")
    @ApiOperation("管户报表统计")
    public DwbRetailResultVO selectCustManagerResult() {
        return gridCmpmService.selectCustManagerResult();
    }

    @GetMapping("/custManager/custLevel/list")
    @Log(title = "绩效网格-管户报表星级列表")
    @ApiOperation("管户报表星级列表")
    public AjaxResult getCustLevelListForManager() {
        return success(gridCmpmService.getCustLevelListForManager());
    }

    @PostMapping("/custManager/export")
    @Log(title = "绩效网格-管户报表异步导出", businessType = BusinessType.EXPORT)
    @ApiOperation("管户报表异步导出")
    public AjaxResult exportCustManager(@RequestBody(required = false) CustManagerDTO custManagerDTO) {
        if (custManagerDTO == null) {
            custManagerDTO = new CustManagerDTO();
        }
        String taskId = gridCmpmService.exportCustManagerAsync(custManagerDTO);
        return AjaxResult.success("导出任务创建成功，请稍后前往下载中心下载", taskId);
    }

    @GetMapping("/custLevel/count")
    @Log(title = "绩效网格-查询客户分层等级")
    @ApiOperation("查询客户分层等级")
    public AjaxResult custLevelCount(CustManagerDTO custManagerDTO) {
        return success(gridCmpmService.custLevelCount(custManagerDTO));
    }

    @PostMapping("/custBaseInfo/list")
    @Log(title = "绩效网格-我的客户绩效网格查询")
    @ApiOperation("我的客户绩效网格查询")
    public AjaxResult selectCustBaseInfoList(@RequestBody CustBaseInfo custBaseInfo) {
        return AjaxResult.success( gridCmpmCustService.selectCustInfoList (custBaseInfo)) ;
    }

}
