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

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.smsSendconfig.vo.SmsSendconfigPageReqVO;
import com.yc.ship.module.trade.controller.admin.smsSendconfig.vo.SmsSendconfigQueryRespVO;
import com.yc.ship.module.trade.controller.admin.smsSendconfig.vo.SmsSendconfigRespVO;
import com.yc.ship.module.trade.controller.admin.smsSendconfig.vo.SmsSendconfigSaveReqVO;
import com.yc.ship.module.trade.dal.dataobject.smsSendconfig.SmsSendconfigDO;
import com.yc.ship.module.trade.service.smsSendconfig.SmsSendconfigService;
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-sendconfig")
@Validated
public class SmsSendconfigController {

    @Resource
    private SmsSendconfigService smsSendconfigService;

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

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

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

    @GetMapping("/get")
    @Operation(summary = "获得短信发送配置")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
//    @PreAuthorize("@ss.hasPermission('support:sms-sendconfig:query')")
    public CommonResult<SmsSendconfigRespVO> getSmsSendconfig(@RequestParam("id") Long id) {
        SmsSendconfigDO smsSendconfig = smsSendconfigService.getSmsSendconfig(id);
        return success(BeanUtils.toBean(smsSendconfig, SmsSendconfigRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得短信发送配置分页")
    public CommonResult<PageResult<SmsSendconfigRespVO>> getSmsSendconfigPage(@Valid SmsSendconfigPageReqVO pageReqVO) {
        PageResult<SmsSendconfigDO> pageResult = smsSendconfigService.getSmsSendconfigPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, SmsSendconfigRespVO.class));
    }

    @GetMapping("/sendLogPage")
    @Operation(summary = "获得短信发送记录分页")
    public CommonResult<PageResult<SmsSendconfigQueryRespVO>> querySendLogList(@Valid SmsSendconfigPageReqVO pageReqVO) {
        PageResult<SmsSendconfigQueryRespVO> pageResult = smsSendconfigService.querySendLogList(pageReqVO);
        return success(pageResult);
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出短信发送配置 Excel")
    @PreAuthorize("@ss.hasPermission('support:sms-sendconfig:export')")
    @OperateLog(type = EXPORT,enable = false)
    public void exportSmsSendconfigExcel(@Valid SmsSendconfigPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<SmsSendconfigDO> list = smsSendconfigService.getSmsSendconfigPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "短信发送配置.xls", "数据", SmsSendconfigRespVO.class,
                        BeanUtils.toBean(list, SmsSendconfigRespVO.class));
    }

}