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

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.smsSendLog.vo.SmsSendLogPageReqVO;
import com.yc.ship.module.trade.controller.admin.smsSendLog.vo.SmsSendLogRespVO;
import com.yc.ship.module.trade.controller.admin.smsSendLog.vo.SmsSendLogSaveReqVO;
import com.yc.ship.module.trade.dal.dataobject.smsSendLog.SmsSendLogDO;
import com.yc.ship.module.trade.service.smsSendLog.SmsSendLogService;
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("/trade/sms-send-log")
@Validated
public class SmsSendLogController {

    @Resource
    private SmsSendLogService smsSendLogService;

    @PostMapping("/create")
    @Operation(summary = "创建短信发送配置")
    public CommonResult<Long> createSmsSendLog(@Valid @RequestBody SmsSendLogSaveReqVO createReqVO) {
        return success(smsSendLogService.createSmsSendLog(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新短信发送配置")
    public CommonResult<Boolean> updateSmsSendLog(@Valid @RequestBody SmsSendLogSaveReqVO updateReqVO) {
        smsSendLogService.updateSmsSendLog(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除短信发送配置")
    @Parameter(name = "id", description = "编号", required = true)
    public CommonResult<Boolean> deleteSmsSendLog(@RequestParam("id") Long id) {
        smsSendLogService.deleteSmsSendLog(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得短信发送配置")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<SmsSendLogRespVO> getSmsSendLog(@RequestParam("id") Long id) {
        SmsSendLogDO smsSendLog = smsSendLogService.getSmsSendLog(id);
        return success(BeanUtils.toBean(smsSendLog, SmsSendLogRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得短信发送配置分页")
    public CommonResult<PageResult<SmsSendLogRespVO>> getSmsSendLogPage(@Valid SmsSendLogPageReqVO pageReqVO) {
        PageResult<SmsSendLogDO> pageResult = smsSendLogService.getSmsSendLogPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, SmsSendLogRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出短信发送配置 Excel")
    @OperateLog(type = EXPORT,enable = false)
    public void exportSmsSendLogExcel(@Valid SmsSendLogPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<SmsSendLogDO> list = smsSendLogService.getSmsSendLogPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "短信发送配置.xls", "数据", SmsSendLogRespVO.class,
                        BeanUtils.toBean(list, SmsSendLogRespVO.class));
    }

}