package com.yc.ship.module.otc.controller.admin.storecreditlog;

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.otc.controller.admin.storecreditlog.vo.StoreCreditLogPageReqVO;
import com.yc.ship.module.otc.controller.admin.storecreditlog.vo.StoreCreditLogRespVO;
import com.yc.ship.module.otc.controller.admin.storecreditlog.vo.StoreCreditLogSaveReqVO;
import com.yc.ship.module.otc.convert.store.StoreConvert;
import com.yc.ship.module.otc.dal.dataobject.store.StoreDO;
import com.yc.ship.module.otc.dal.dataobject.storecreditlog.StoreCreditLogDO;
import com.yc.ship.module.otc.service.store.StoreService;
import com.yc.ship.module.otc.service.storecreditlog.StoreCreditLogService;
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("/otc/store-credit-log")
@Validated
public class StoreCreditLogController {

    @Resource
    private StoreService storeService;

    @Resource
    private StoreCreditLogService storeCreditLogService;

    @PostMapping("/create")
    @Operation(summary = "创建旅行社门店授信额度变更")
    @PreAuthorize("@ss.hasPermission('otc:store-credit-log:create')")
    public CommonResult<Long> createStoreCreditLog(@Valid @RequestBody StoreCreditLogSaveReqVO createReqVO) {
        return success(storeCreditLogService.createStoreCreditLog(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新旅行社门店授信额度变更")
    @PreAuthorize("@ss.hasPermission('otc:store-credit-log:update')")
    public CommonResult<Boolean> updateStoreCreditLog(@Valid @RequestBody StoreCreditLogSaveReqVO updateReqVO) {
        storeCreditLogService.updateStoreCreditLog(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除旅行社门店授信额度变更")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('otc:store-credit-log:delete')")
    public CommonResult<Boolean> deleteStoreCreditLog(@RequestParam("id") Long id) {
        storeCreditLogService.deleteStoreCreditLog(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得旅行社门店授信额度变更")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
//    @PreAuthorize("@ss.hasPermission('otc:store-credit-log:query')")
    public CommonResult<StoreCreditLogRespVO> getStoreCreditLog(@RequestParam("id") Long id) {
        StoreCreditLogDO storeCreditLog = storeCreditLogService.getStoreCreditLog(id);
        return success(BeanUtils.toBean(storeCreditLog, StoreCreditLogRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得旅行社门店授信额度变更分页")
//    @PreAuthorize("@ss.hasPermission('otc:store-credit-log:query')")
    public CommonResult<PageResult<StoreCreditLogRespVO>> getStoreCreditLogPage(@Valid StoreCreditLogPageReqVO pageReqVO) {
        PageResult<StoreCreditLogDO> pageResult = storeCreditLogService.getStoreCreditLogPage(pageReqVO);
        List<StoreDO> stores = storeService.getSotreList(convertList(pageResult.getList(), StoreCreditLogDO::getStoreId));
        Map<Long, StoreDO> storeMap = CollectionUtils.convertMap(stores, StoreDO::getId);
        return success(new PageResult<>(StoreConvert.INSTANCE.convertStoreCreditLogList(pageResult.getList(), storeMap),
                pageResult.getTotal()));
//        return success(BeanUtils.toBean(pageResult, StoreCreditLogRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出旅行社门店授信额度变更 Excel")
    @PreAuthorize("@ss.hasPermission('otc:store-credit-log:export')")
    @OperateLog(type = EXPORT,enable = false)
    public void exportStoreCreditLogExcel(@Valid StoreCreditLogPageReqVO pageReqVO,
                                          HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<StoreCreditLogDO> list = storeCreditLogService.getStoreCreditLogPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "旅行社门店授信额度变更.xls", "数据", StoreCreditLogRespVO.class,
                BeanUtils.toBean(list, StoreCreditLogRespVO.class));
    }

}
