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

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.storecard.vo.StoreCardPageReqVO;
import com.yc.ship.module.otc.controller.admin.storecard.vo.StoreCardRespVO;
import com.yc.ship.module.otc.controller.admin.storecard.vo.StoreCardSaveReqVO;
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.storecard.StoreCardDO;
import com.yc.ship.module.otc.service.store.StoreService;
import com.yc.ship.module.otc.service.storecard.StoreCardService;
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-card")
@Validated
public class StoreCardController {

    @Resource
    private StoreService storeService;

    @Resource
    private StoreCardService storeCardService;


    @PostMapping("/create")
    @Operation(summary = "创建门店资金卡")
    @PreAuthorize("@ss.hasPermission('otc:store-card:create')")
    public CommonResult<Long> createStoreCard(@Valid @RequestBody StoreCardSaveReqVO createReqVO) {
        return success(storeCardService.createStoreCard(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新门店资金卡")
    @PreAuthorize("@ss.hasPermission('otc:store-card:update')")
    public CommonResult<Boolean> updateStoreCard(@Valid @RequestBody StoreCardSaveReqVO updateReqVO) {
        storeCardService.updateStoreCard(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除门店资金卡")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('otc:store-card:delete')")
    public CommonResult<Boolean> deleteStoreCard(@RequestParam("id") Long id) {
        storeCardService.deleteStoreCard(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得门店资金卡")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
//    @PreAuthorize("@ss.hasPermission('otc:store-card:query')")
    public CommonResult<StoreCardRespVO> getStoreCard(@RequestParam("id") Long id) {
        StoreCardDO storeCard = storeCardService.getStoreCard(id);
        return success(BeanUtils.toBean(storeCard, StoreCardRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得门店资金卡分页")
//    @PreAuthorize("@ss.hasPermission('otc:store-card:query')")
    public CommonResult<PageResult<StoreCardRespVO>> getStoreCardPage(@Valid StoreCardPageReqVO pageReqVO) {
        PageResult<StoreCardDO> pageResult = storeCardService.getStoreCardPage(pageReqVO);
        List<StoreDO> stores = storeService.getSotreList(convertList(pageResult.getList(), StoreCardDO::getStoreId));
        Map<Long, StoreDO> storeMap = CollectionUtils.convertMap(stores, StoreDO::getId);
        return success(new PageResult<>(StoreConvert.INSTANCE.convertStoreCardList(pageResult.getList(), storeMap),
                pageResult.getTotal()));
//        return success(BeanUtils.toBean(pageResult, StoreCardRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出门店资金卡 Excel")
    @PreAuthorize("@ss.hasPermission('otc:store-card:export')")
    @OperateLog(type = EXPORT,enable = false)
    public void exportStoreCardExcel(@Valid StoreCardPageReqVO pageReqVO,
                                     HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<StoreCardDO> list = storeCardService.getStoreCardPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "门店资金卡.xls", "数据", StoreCardRespVO.class,
                BeanUtils.toBean(list, StoreCardRespVO.class));
    }

}
