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

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.distributorauditlog.vo.DistributorAuditLogPageReqVO;
import com.yc.ship.module.ota.controller.admin.distributorauditlog.vo.DistributorAuditLogRespVO;
import com.yc.ship.module.ota.controller.admin.distributorauditlog.vo.DistributorAuditLogSaveReqVO;
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.distributorauditlog.DistributorAuditLogDO;
import com.yc.ship.module.ota.service.distributor.DistributorService;
import com.yc.ship.module.ota.service.distributorauditlog.DistributorAuditLogService;
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-log")
@Validated
public class DistributorAuditLogController {

    @Resource
    private DistributorService distributorService;

    @Resource
    private DistributorAuditLogService distributorAuditLogService;

    @PostMapping("/create")
    @Operation(summary = "创建分销商授信额度变更")
    @PreAuthorize("@ss.hasPermission('ota:distributor-audit-log:create')")
    public CommonResult<Long> createDistributorAuditLog(@Valid @RequestBody DistributorAuditLogSaveReqVO createReqVO) {
        return success(distributorAuditLogService.createDistributorAuditLog(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新分销商授信额度变更")
    @PreAuthorize("@ss.hasPermission('ota:distributor-audit-log:update')")
    public CommonResult<Boolean> updateDistributorAuditLog(@Valid @RequestBody DistributorAuditLogSaveReqVO updateReqVO) {
        distributorAuditLogService.updateDistributorAuditLog(updateReqVO);
        return success(true);
    }

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

    @GetMapping("/get")
    @Operation(summary = "获得分销商授信额度变更")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('ota:distributor-audit-log:query')")
    public CommonResult<DistributorAuditLogRespVO> getDistributorAuditLog(@RequestParam("id") Long id) {
        DistributorAuditLogDO distributorAuditLog = distributorAuditLogService.getDistributorAuditLog(id);
        return success(BeanUtils.toBean(distributorAuditLog, DistributorAuditLogRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得分销商授信额度变更分页")
    @PreAuthorize("@ss.hasPermission('ota:distributor-audit-log:query')")
    public CommonResult<PageResult<DistributorAuditLogRespVO>> getDistributorAuditLogPage(@Valid DistributorAuditLogPageReqVO pageReqVO) {
        PageResult<DistributorAuditLogDO> pageResult = distributorAuditLogService.getDistributorAuditLogPage(pageReqVO);
        List<DistributorDO> distributorList = distributorService.getDistributorList(convertList(pageResult.getList(), DistributorAuditLogDO::getDistributorId));
        Map<Long, DistributorDO> distributorMap = CollectionUtils.convertMap(distributorList, DistributorDO::getId);
        return success(new PageResult<>(DistributorConvert.INSTANCE.convertAuditLogList(pageResult.getList(), distributorMap),
                pageResult.getTotal()));
//        return success(BeanUtils.toBean(pageResult, DistributorAuditLogRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出分销商授信额度变更 Excel")
    @PreAuthorize("@ss.hasPermission('ota:distributor-audit-log:export')")
    @OperateLog(type = EXPORT,enable = false)
    public void exportDistributorAuditLogExcel(@Valid DistributorAuditLogPageReqVO pageReqVO,
                                               HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<DistributorAuditLogDO> list = distributorAuditLogService.getDistributorAuditLogPage(pageReqVO).getList();
        List<DistributorDO> distributorList = distributorService.getDistributorList(convertList(list, DistributorAuditLogDO::getDistributorId));
        Map<Long, DistributorDO> distributorMap = CollectionUtils.convertMap(distributorList, DistributorDO::getId);
        List<DistributorAuditLogRespVO> auditLogRespVOS = DistributorConvert.INSTANCE.convertAuditLogList(list, distributorMap);
        // 导出 Excel
        ExcelUtils.write(response, "分销商授信额度变更.xls", "数据", DistributorAuditLogRespVO.class, auditLogRespVOS);
    }

}
