package com.sqx.modules.errand.controller;

import cn.hutool.core.util.ObjectUtil;
import com.sqx.common.exception.SqxException;
import com.sqx.common.utils.PageUtils;
import com.sqx.common.utils.Result;
import com.sqx.modules.errand.dto.SmsCountQueryDTO;
import com.sqx.modules.errand.dto.SmsLogQueryDTO;
import com.sqx.modules.errand.dto.SmsTemplateDTO;
import com.sqx.modules.errand.dto.SmsTemplateQueryDTO;
import com.sqx.modules.errand.service.TbIndentSmsSendLogService;
import com.sqx.modules.errand.service.TbIndentSmsTemplateService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.Arrays;

@RestController
@RequestMapping("admin/tb-indent-sms")
@Api(value = "管理端-跑腿确认送达短信", tags = {"管理端-跑腿确认送达短信"})
@RequiredArgsConstructor
public class TbIndentSmsController {

    private final TbIndentSmsSendLogService smsSendLogService;
    private final TbIndentSmsTemplateService smsTemplateService;

    @ApiOperation("模板分页")
    @GetMapping(value = "template")
    public Result templatePage(SmsTemplateQueryDTO queryDTO) {
        PageUtils pageUtils = smsTemplateService.templatePage(queryDTO);
        return Result.success().put("data", pageUtils);
    }

    @ApiOperation("新增模板")
    @PostMapping(value = "template")
    public Result addTemplate(@Valid @RequestBody SmsTemplateDTO template) {
        smsTemplateService.saveTemplate(template);
        return Result.success();
    }

    @ApiOperation("修改模板")
    @PutMapping(value = "template")
    public Result updateTemplate(@Valid @RequestBody SmsTemplateDTO template) {
        if (ObjectUtil.isNull(template.getId())) {
            throw new SqxException("模板id不能为空");
        }
        smsTemplateService.updateTemplate(template);
        return Result.success();
    }

    @ApiOperation("删除模板")
    @DeleteMapping(value = "template")
    public Result delTemplates(@RequestBody Long[] ids) {
        smsTemplateService.removeByIds(Arrays.asList(ids));
        return Result.success();
    }

    @ApiOperation("短信发送记录分页")
    @GetMapping(value = "log")
    public Result logPage(SmsLogQueryDTO queryDTO) {
        PageUtils pageUtils = smsSendLogService.logPage(queryDTO);
        return Result.success().put("data", pageUtils);
    }

    @ApiOperation("短信发送成功数量统计")
    @GetMapping(value = "count-of-send-success")
    public Result countOfSendSuccess(SmsCountQueryDTO queryDTO) {
        int count = smsSendLogService.countOfSendSuccess(queryDTO);
        return Result.success().put("data", count);
    }

    @ApiOperation("删除短信发送记录")
    @DeleteMapping(value = "log")
    public Result delLogs(@RequestBody Long[] ids) {
        smsSendLogService.removeByIds(Arrays.asList(ids));
        return Result.success();
    }

    @ApiOperation("获取短信金额配置")
    @GetMapping(value = "price-of-sms")
    public Result getPriceOfSms() {
        Double price = smsTemplateService.getPriceOfSms();
        return Result.success().put("data", price);
    }
}
