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

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.framework.tenant.core.aop.TenantIgnore;
import com.yc.ship.framework.tenant.core.context.TenantContextHolder;
import com.yc.ship.module.otc.controller.admin.storeaudit.vo.StoreAuditPageReqVO;
import com.yc.ship.module.otc.controller.admin.storeaudit.vo.StoreAuditRespVO;
import com.yc.ship.module.otc.controller.admin.storeaudit.vo.StoreAuditSaveReqVO;
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.storeaudit.StoreAuditDO;
import com.yc.ship.module.otc.service.store.StoreService;
import com.yc.ship.module.otc.service.storeaudit.StoreAuditService;
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-audit")
@Validated
public class StoreAuditController {

    @Resource
    private StoreService storeService;

    @Resource
    private StoreAuditService storeAuditService;

    @PostMapping("/create")
    @Operation(summary = "创建旅行社门店授信提额/授信充值/余额充值")
    @PreAuthorize("@ss.hasPermission('otc:store-audit:create')")
    public CommonResult<Long> createStoreAudit(@Valid @RequestBody StoreAuditSaveReqVO createReqVO) {
        return success(storeAuditService.createStoreAudit(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新旅行社门店授信提额/授信充值/余额充值")
    @PreAuthorize("@ss.hasPermission('otc:store-audit:update')")
    public CommonResult<Boolean> updateStoreAudit(@Valid @RequestBody StoreAuditSaveReqVO updateReqVO) {
        storeAuditService.updateStoreAudit(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除旅行社门店授信提额/授信充值/余额充值")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('otc:store-audit:delete')")
    public CommonResult<Boolean> deleteStoreAudit(@RequestParam("id") Long id) {
        storeAuditService.deleteStoreAudit(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得旅行社门店授信提额/授信充值/余额充值")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('otc:store-audit:query')")
    public CommonResult<StoreAuditRespVO> getStoreAudit(@RequestParam("id") Long id) {
        StoreAuditDO storeAudit = storeAuditService.getStoreAudit(id);
        return success(BeanUtils.toBean(storeAudit, StoreAuditRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得旅行社门店授信提额/授信充值/余额充值分页")
    @PreAuthorize("@ss.hasPermission('otc:store-audit:query')")
    public CommonResult<PageResult<StoreAuditRespVO>> getStoreAuditPage(@Valid StoreAuditPageReqVO pageReqVO) {
        PageResult<StoreAuditDO> pageResult = storeAuditService.getStoreAuditPage(pageReqVO);
        List<StoreDO> stores = storeService.getSotreList(convertList(pageResult.getList(), StoreAuditDO::getStoreId));
        Map<Long, StoreDO> storeMap = CollectionUtils.convertMap(stores, StoreDO::getId);
        return success(new PageResult<>(StoreConvert.INSTANCE.convertStoreAuditList(pageResult.getList(), storeMap),
                pageResult.getTotal()));
//        return success(BeanUtils.toBean(pageResult, StoreAuditRespVO.class));
    }

    @GetMapping("/plat/page")
    @Operation(summary = "获得旅行社门店授信提额/授信充值/余额充值分页")
    @PreAuthorize("@ss.hasPermission('otc:store-audit:query')")
    @TenantIgnore
    public CommonResult<PageResult<StoreAuditRespVO>> getPlatStoreAuditPage(@Valid StoreAuditPageReqVO pageReqVO) {
        pageReqVO.setPlatTenantId(TenantContextHolder.getTenantId());
        PageResult<StoreAuditRespVO> pageResult = storeAuditService.getPlatStoreAuditPage(pageReqVO);
        return success(pageResult);
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出旅行社门店授信提额/授信充值/余额充值 Excel")
    @PreAuthorize("@ss.hasPermission('otc:store-audit:export')")
    @OperateLog(type = EXPORT,enable = false)
    public void exportStoreAuditExcel(@Valid StoreAuditPageReqVO pageReqVO,
                                      HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<StoreAuditDO> list = storeAuditService.getStoreAuditPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "旅行社门店授信提额/授信充值/余额充值.xls", "数据", StoreAuditRespVO.class,
                BeanUtils.toBean(list, StoreAuditRespVO.class));
    }

}
