package com.yc.ship.module.ota.controller.admin.distributoraudit;

import com.yc.ship.framework.common.pojo.CommonResult;
import com.yc.ship.framework.common.pojo.PageParam;
import com.yc.ship.framework.common.pojo.PageResult;
import com.yc.ship.framework.common.util.collection.CollectionUtils;
import com.yc.ship.framework.common.util.object.BeanUtils;
import com.yc.ship.framework.excel.core.util.ExcelUtils;
import com.yc.ship.framework.operatelog.core.annotations.OperateLog;
import com.yc.ship.module.ota.controller.admin.distributoraudit.vo.DistributorAuditPageReqVO;
import com.yc.ship.module.ota.controller.admin.distributoraudit.vo.DistributorAuditRespVO;
import com.yc.ship.module.ota.controller.admin.distributoraudit.vo.DistributorAuditSaveReqVO;
import com.yc.ship.module.ota.convert.distrbutor.DistributorConvert;
import com.yc.ship.module.ota.dal.dataobject.distributor.DistributorDO;
import com.yc.ship.module.ota.dal.dataobject.distributoraudit.DistributorAuditDO;
import com.yc.ship.module.ota.service.distributor.DistributorService;
import com.yc.ship.module.ota.service.distributoraudit.DistributorAuditService;
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.security.access.prepost.PreAuthorize;
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 java.util.Map;

import static com.yc.ship.framework.common.pojo.CommonResult.success;
import static com.yc.ship.framework.common.util.collection.CollectionUtils.convertList;
import static com.yc.ship.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;

@Tag(name = "管理后台 - 分销商授信提额/授信充值/余额充值")
@RestController
@RequestMapping("/distributor-audit")
@Validated
public class DistributorAuditController {

    @Resource
    private DistributorService distributorService;

    @Resource
    private DistributorAuditService distributorAuditService;

    @PostMapping("/create")
    @Operation(summary = "创建分销商授信提额/授信充值/余额充值")
    @PreAuthorize("@ss.hasPermission('ota:distributor-audit:create')")
    public CommonResult<Long> createDistributorAudit(@Valid @RequestBody DistributorAuditSaveReqVO createReqVO) {
        return success(distributorAuditService.createDistributorAudit(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新分销商授信提额/授信充值/余额充值")
    @PreAuthorize("@ss.hasPermission('ota:distributor-audit:update')")
    public CommonResult<Boolean> updateDistributorAudit(@Valid @RequestBody DistributorAuditSaveReqVO updateReqVO) {
        distributorAuditService.updateDistributorAudit(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除分销商授信提额/授信充值/余额充值")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('ota:distributor-audit:delete')")
    public CommonResult<Boolean> deleteDistributorAudit(@RequestParam("id") Long id) {
        distributorAuditService.deleteDistributorAudit(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得分销商授信提额/授信充值/余额充值")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('ota:distributor-audit:query')")
    public CommonResult<DistributorAuditRespVO> getDistributorAudit(@RequestParam("id") Long id) {
        DistributorAuditDO distributorAudit = distributorAuditService.getDistributorAudit(id);
        return success(BeanUtils.toBean(distributorAudit, DistributorAuditRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得分销商授信提额/授信充值/余额充值分页")
    @PreAuthorize("@ss.hasPermission('ota:distributor-audit:query')")
    public CommonResult<PageResult<DistributorAuditRespVO>> getDistributorAuditPage(@Valid DistributorAuditPageReqVO pageReqVO) {
        PageResult<DistributorAuditDO> pageResult = distributorAuditService.getDistributorAuditPage(pageReqVO);
        List<DistributorDO> distributorList = distributorService.getDistributorList(convertList(pageResult.getList(), DistributorAuditDO::getDistributorId));
        Map<Long, DistributorDO> distributorMap = CollectionUtils.convertMap(distributorList, DistributorDO::getId);
        return success(new PageResult<>(DistributorConvert.INSTANCE.convertAuditList(pageResult.getList(), distributorMap),
                pageResult.getTotal()));
//        return success(BeanUtils.toBean(pageResult, DistributorAuditRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出分销商授信提额/授信充值/余额充值 Excel")
    @PreAuthorize("@ss.hasPermission('ota:distributor-audit:export')")
    @OperateLog(type = EXPORT,enable = false)
    public void exportDistributorAuditExcel(@Valid DistributorAuditPageReqVO pageReqVO,
                                            HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<DistributorAuditDO> list = distributorAuditService.getDistributorAuditPage(pageReqVO).getList();
        List<DistributorDO> distributorList = distributorService.getDistributorList(convertList(list, DistributorAuditDO::getDistributorId));
        Map<Long, DistributorDO> distributorMap = CollectionUtils.convertMap(distributorList, DistributorDO::getId);
        List<DistributorAuditRespVO> auditRespVOS = DistributorConvert.INSTANCE.convertAuditList(list, distributorMap);
        // 导出 Excel
        ExcelUtils.write(response, "分销商授信提额/授信充值/余额充值.xls", "数据", DistributorAuditRespVO.class, auditRespVOS);
    }

}
