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

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.smsProduct.vo.SmsProductPageReqVO;
import com.yc.ship.module.trade.controller.admin.smsProduct.vo.SmsProductRespVO;
import com.yc.ship.module.trade.controller.admin.smsProduct.vo.SmsProductSaveReqVO;
import com.yc.ship.module.trade.dal.dataobject.smsProduct.SmsProductDO;
import com.yc.ship.module.trade.service.smsProduct.SmsProductService;
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 static com.yc.ship.framework.common.pojo.CommonResult.success;
import static com.yc.ship.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;


@Tag(name = "管理后台 - 短信发送配置关联")
@RestController
@RequestMapping("/trade/sms-product")
@Validated
public class SmsProductController {

    @Resource
    private SmsProductService smsProductService;

    @PostMapping("/create")
    @Operation(summary = "创建短信发送配置关联")
    @PreAuthorize("@ss.hasPermission('support:sms-product:create')")
    public CommonResult<Long> createSmsProduct(@Valid @RequestBody SmsProductSaveReqVO createReqVO) {
        return success(smsProductService.createSmsProduct(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新短信发送配置关联")
    @PreAuthorize("@ss.hasPermission('support:sms-product:update')")
    public CommonResult<Boolean> updateSmsProduct(@Valid @RequestBody SmsProductSaveReqVO updateReqVO) {
        smsProductService.updateSmsProduct(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除短信发送配置关联")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('support:sms-product:delete')")
    public CommonResult<Boolean> deleteSmsProduct(@RequestParam("id") Long id) {
        smsProductService.deleteSmsProduct(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得短信发送配置关联")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('support:sms-product:query')")
    public CommonResult<SmsProductRespVO> getSmsProduct(@RequestParam("id") Long id) {
        SmsProductDO smsProduct = smsProductService.getSmsProduct(id);
        return success(BeanUtils.toBean(smsProduct, SmsProductRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得短信发送配置关联分页")
    @PreAuthorize("@ss.hasPermission('support:sms-product:query')")
    public CommonResult<PageResult<SmsProductRespVO>> getSmsProductPage(@Valid SmsProductPageReqVO pageReqVO) {
        PageResult<SmsProductDO> pageResult = smsProductService.getSmsProductPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, SmsProductRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出短信发送配置关联 Excel")
    @PreAuthorize("@ss.hasPermission('support:sms-product:export')")
    @OperateLog(type = EXPORT,enable = false)
    public void exportSmsProductExcel(@Valid SmsProductPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<SmsProductDO> list = smsProductService.getSmsProductPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "短信发送配置关联.xls", "数据", SmsProductRespVO.class,
                        BeanUtils.toBean(list, SmsProductRespVO.class));
    }

}