package com.yc.ship.module.trade.controller.admin.billrule;

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.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.trade.controller.admin.billrule.vo.BillRulePageReqVO;
import com.yc.ship.module.trade.controller.admin.billrule.vo.BillRuleRespVO;
import com.yc.ship.module.trade.controller.admin.billrule.vo.BillRuleSaveReqVO;
import com.yc.ship.module.trade.api.dto.StoreRespVO;
import com.yc.ship.module.ota.controller.admin.distributor.vo.DistributorRespVO;
import com.yc.ship.module.trade.dal.dataobject.billrule.BillRuleDO;
import com.yc.ship.module.ota.dal.dataobject.distributor.DistributorDO;
import com.yc.ship.module.trade.service.billrule.BillRuleService;
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.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 static com.yc.ship.framework.common.pojo.CommonResult.success;
import static com.yc.ship.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;


@Tag(name = "管理后台 - 旅行社账单规则")
@RestController
@RequestMapping("/ota/bill-rule")
@Validated
public class BillRuleController {

    @Resource
    private BillRuleService billRuleService;

    @PostMapping("/create")
    @Operation(summary = "创建旅行社账单规则")
//    @PreAuthorize("@ss.hasPermission('ota:bill-rule:create')")
    public CommonResult<Long> createBillRule(@Valid @RequestBody BillRuleSaveReqVO createReqVO) {
        return success(billRuleService.createBillRule(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新旅行社账单规则")
//    @PreAuthorize("@ss.hasPermission('ota:bill-rule:update')")
    public CommonResult<Boolean> updateBillRule(@Valid @RequestBody BillRuleSaveReqVO updateReqVO) {
        billRuleService.updateBillRule(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除旅行社账单规则")
    @Parameter(name = "id", description = "编号", required = true)
//    @PreAuthorize("@ss.hasPermission('ota:bill-rule:delete')")
    public CommonResult<Boolean> deleteBillRule(@RequestParam("id") Long id) {
        billRuleService.deleteBillRule(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得旅行社账单规则")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
//    @PreAuthorize("@ss.hasPermission('ota:bill-rule:query')")
    public CommonResult<BillRuleRespVO> getBillRule(@RequestParam("id") Long id) {
        BillRuleRespVO billRule = billRuleService.getBillRule(id);
        return success(BeanUtils.toBean(billRule, BillRuleRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得旅行社账单规则分页")
//    @PreAuthorize("@ss.hasPermission('ota:bill-rule:query')")
    public CommonResult<PageResult<BillRuleRespVO>> getBillRulePage(@Valid BillRulePageReqVO pageReqVO) {
        PageResult<BillRuleDO> pageResult = billRuleService.getBillRulePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, BillRuleRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出旅行社账单规则 Excel")
//    @PreAuthorize("@ss.hasPermission('ota:bill-rule:export')")
    @OperateLog(type = EXPORT,enable = false)
    public void exportBillRuleExcel(@Valid BillRulePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<BillRuleDO> list = billRuleService.getBillRulePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "旅行社账单规则.xls", "数据", BillRuleRespVO.class,
                        BeanUtils.toBean(list, BillRuleRespVO.class));
    }

    @GetMapping("/selectDistributorList")
    @Operation(summary = "获得旅行社分销商信息")
    public CommonResult<List<DistributorRespVO>> selectDistributorList(@RequestParam("ids") String ids) {
        List<DistributorDO> list = billRuleService.selectDistributorList(ids);
        return success(BeanUtils.toBean(list, DistributorRespVO.class));
    }

    @GetMapping("/getDistributorList")
    @Operation(summary = "获得旅行社分销商信息(平台，旅行社，门店)")
    public CommonResult<List<DistributorRespVO>> getDistributorList() {
        List<DistributorDO> list = billRuleService.getDistributorList();
        return success(BeanUtils.toBean(list, DistributorRespVO.class));
    }

    @GetMapping("/selectStoreList")
    @Operation(summary = "获得旅行社门店信息")
    public CommonResult<List<StoreRespVO>> selectStoreList(@RequestParam("distributorId") Long distributorId) {
        List<StoreRespVO> list = billRuleService.selectStoreList(distributorId);
        return success(BeanUtils.toBean(list, StoreRespVO.class));
    }

}