package com.yc.ship.module.marketing.controller.admin.policydetail;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import com.yc.ship.framework.common.pojo.PageParam;
import com.yc.ship.framework.common.pojo.PageResult;
import com.yc.ship.framework.common.pojo.CommonResult;
import com.yc.ship.framework.common.util.object.BeanUtils;
import static com.yc.ship.framework.common.pojo.CommonResult.success;

import com.yc.ship.framework.excel.core.util.ExcelUtils;

import com.yc.ship.framework.apilog.core.annotation.ApiAccessLog;
import static com.yc.ship.framework.apilog.core.enums.OperateTypeEnum.*;

import com.yc.ship.module.marketing.controller.admin.policydetail.vo.*;
import com.yc.ship.module.marketing.dal.dataobject.policydetail.PolicyDetailDO;
import com.yc.ship.module.marketing.service.policydetail.PolicyDetailService;

@Tag(name = "管理后台 - 政策详情")
@RestController
@RequestMapping("/marketing/policy-detail")
@Validated
public class PolicyDetailController {

    @Resource
    private PolicyDetailService policyDetailService;

    @PostMapping("/create")
    @Operation(summary = "创建政策详情")
    @PreAuthorize("@ss.hasPermission('marketing:policy-detail:create')")
    public CommonResult<Long> createPolicyDetail(@Valid @RequestBody PolicyDetailSaveReqVO createReqVO) {
        return success(policyDetailService.createPolicyDetail(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新政策详情")
    @PreAuthorize("@ss.hasPermission('marketing:policy-detail:update')")
    public CommonResult<Boolean> updatePolicyDetail(@Valid @RequestBody PolicyDetailSaveReqVO updateReqVO) {
        policyDetailService.updatePolicyDetail(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除政策详情")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('marketing:policy-detail:delete')")
    public CommonResult<Boolean> deletePolicyDetail(@RequestParam("id") Long id) {
        policyDetailService.deletePolicyDetail(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得政策详情")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('marketing:policy-detail:query')")
    public CommonResult<PolicyDetailRespVO> getPolicyDetail(@RequestParam("id") Long id) {
        PolicyDetailDO policyDetail = policyDetailService.getPolicyDetail(id);
        return success(BeanUtils.toBean(policyDetail, PolicyDetailRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得政策详情分页")
    @PreAuthorize("@ss.hasPermission('marketing:policy-detail:query')")
    public CommonResult<PageResult<PolicyDetailRespVO>> getPolicyDetailPage(@Valid PolicyDetailPageReqVO pageReqVO) {
        PageResult<PolicyDetailDO> pageResult = policyDetailService.getPolicyDetailPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, PolicyDetailRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出政策详情 Excel")
    @PreAuthorize("@ss.hasPermission('marketing:policy-detail:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportPolicyDetailExcel(@Valid PolicyDetailPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<PolicyDetailDO> list = policyDetailService.getPolicyDetailPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "政策详情.xls", "数据", PolicyDetailRespVO.class,
                        BeanUtils.toBean(list, PolicyDetailRespVO.class));
    }

}