package cn.iocoder.yudao.module.crm.controller.admin.salesperformancesettlement;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import cn.iocoder.yudao.module.crm.controller.admin.salesperformancesettlement.vo.SalesPerformanceSettlementPageReqVO;
import cn.iocoder.yudao.module.crm.controller.admin.salesperformancesettlement.vo.SalesPerformanceSettlementRespVO;
import cn.iocoder.yudao.module.crm.controller.admin.salesperformancesettlement.vo.SalesPerformanceSettlementSaveReqVO;
import cn.iocoder.yudao.module.crm.dal.dataobject.salesperformancesettlement.SalesPerformanceSettlementDO;
import cn.iocoder.yudao.module.crm.service.salesperformancesettlement.SalesPerformanceSettlementService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;

@Tag(name = "管理后台 - 销售业绩结算记录")
@RestController
@RequestMapping("/crm/sales-performance-settlement")
@Validated
public class SalesPerformanceSettlementController {

    @Resource
    private SalesPerformanceSettlementService salesPerformanceSettlementService;


    @PutMapping("/update")
    @Operation(summary = "更新销售业绩结算记录")
    public CommonResult<Boolean> updateSalesPerformanceSettlement(@Valid @RequestBody SalesPerformanceSettlementSaveReqVO updateReqVO) {
        salesPerformanceSettlementService.updateSalesPerformanceSettlement(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除销售业绩结算记录")
    @Parameter(name = "id", description = "编号", required = true)
    public CommonResult<Boolean> deleteSalesPerformanceSettlement(@RequestParam("id") Long id) {
        salesPerformanceSettlementService.deleteSalesPerformanceSettlement(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得销售业绩结算记录")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<SalesPerformanceSettlementRespVO> getSalesPerformanceSettlement(@RequestParam("id") Long id) {
        SalesPerformanceSettlementDO salesPerformanceSettlement = salesPerformanceSettlementService.getSalesPerformanceSettlement(id);
        return success(BeanUtils.toBean(salesPerformanceSettlement, SalesPerformanceSettlementRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得销售业绩结算记录分页")
    public CommonResult<PageResult<SalesPerformanceSettlementRespVO>> getSalesPerformanceSettlementPage(@Valid SalesPerformanceSettlementPageReqVO pageReqVO) {
        PageResult<SalesPerformanceSettlementDO> pageResult = salesPerformanceSettlementService.getSalesPerformanceSettlementPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, SalesPerformanceSettlementRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出销售业绩结算记录 Excel")
    @OperateLog(type = EXPORT)
    public void exportSalesPerformanceSettlementExcel(@Valid SalesPerformanceSettlementPageReqVO pageReqVO,
                                                      HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<SalesPerformanceSettlementDO> list = salesPerformanceSettlementService.getSalesPerformanceSettlementPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "销售业绩结算记录.xls", "数据", SalesPerformanceSettlementRespVO.class,
                BeanUtils.toBean(list, SalesPerformanceSettlementRespVO.class));
    }

}
