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

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.tradelog.vo.TradeLogPageReqVO;
import com.yc.ship.module.ota.controller.admin.tradelog.vo.TradeLogRespVO;
import com.yc.ship.module.ota.controller.admin.tradelog.vo.TradeLogSaveReqVO;
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.tradelog.TradeLogDO;
import com.yc.ship.module.ota.service.distributor.DistributorService;
import com.yc.ship.module.ota.service.tradelog.TradeLogService;
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("/trade-log")
@Validated
public class TradeLogController {

    @Resource
    private TradeLogService tradeLogService;

    @Resource
    private DistributorService distributorService;

    @PostMapping("/create")
    @Operation(summary = "创建分销商交易日志")
    @PreAuthorize("@ss.hasPermission('ota:trade-log:create')")
    public CommonResult<Long> createTradeLog(@Valid @RequestBody TradeLogSaveReqVO createReqVO) {
        return success(tradeLogService.createTradeLog(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新分销商交易日志")
    @PreAuthorize("@ss.hasPermission('ota:trade-log:update')")
    public CommonResult<Boolean> updateTradeLog(@Valid @RequestBody TradeLogSaveReqVO updateReqVO) {
        tradeLogService.updateTradeLog(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除分销商交易日志")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('ota:trade-log:delete')")
    public CommonResult<Boolean> deleteTradeLog(@RequestParam("id") Long id) {
        tradeLogService.deleteTradeLog(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得分销商交易日志")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
//    @PreAuthorize("@ss.hasPermission('ota:trade-log:query')")
    public CommonResult<TradeLogRespVO> getTradeLog(@RequestParam("id") Long id) {
        TradeLogDO tradeLog = tradeLogService.getTradeLog(id);
        return success(BeanUtils.toBean(tradeLog, TradeLogRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得分销商交易日志分页")
//    @PreAuthorize("@ss.hasPermission('ota:trade-log:query')")
    public CommonResult<PageResult<TradeLogRespVO>> getTradeLogPage(@Valid TradeLogPageReqVO pageReqVO) {
        PageResult<TradeLogDO> pageResult = tradeLogService.getTradeLogPage(pageReqVO);
        List<DistributorDO> distributorList = distributorService.getDistributorList(convertList(pageResult.getList(), TradeLogDO::getDistributorId));
        Map<Long, DistributorDO> distributorMap = CollectionUtils.convertMap(distributorList, DistributorDO::getId);
        return success(new PageResult<>(DistributorConvert.INSTANCE.convertTradeLogList(pageResult.getList(), distributorMap),
                pageResult.getTotal()));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出分销商交易日志 Excel")
    @PreAuthorize("@ss.hasPermission('ota:trade-log:export')")
    @OperateLog(type = EXPORT,enable = false)
    public void exportTradeLogExcel(@Valid TradeLogPageReqVO pageReqVO, HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<TradeLogDO> list = tradeLogService.getTradeLogPage(pageReqVO).getList();
        List<DistributorDO> distributorList = distributorService.getDistributorList(convertList(list, TradeLogDO::getDistributorId));
        Map<Long, DistributorDO> distributorMap = CollectionUtils.convertMap(distributorList, DistributorDO::getId);
        List<TradeLogRespVO> respVOS = DistributorConvert.INSTANCE.convertTradeLogList(list, distributorMap);
        // 导出 Excel
        ExcelUtils.write(response, "分销商交易日志.xls", "数据", TradeLogRespVO.class, respVOS);
    }

}
