package com.hzs.third.sms.controller.manage;

import com.hzs.common.core.annotation.Log;
import com.hzs.common.core.constant.msg.CommonMsgConstants;
import com.hzs.common.core.enums.*;
import com.hzs.common.core.utils.poi.ExcelUtil;
import com.hzs.common.core.web.controller.BaseController;
import com.hzs.common.core.web.domain.AjaxResult;
import com.hzs.common.core.web.page.TableDataInfo;
import com.hzs.common.domain.third.sms.ext.TSmsRecordExt;
import com.hzs.common.security.utils.SecurityUtils;
import com.hzs.common.util.TransactionUtils;
import com.hzs.third.sms.param.SmsCreateParam;
import com.hzs.third.sms.param.SmsRecordQueryParam;
import com.hzs.third.sms.service.ITSmsRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 短信发送记录
 */
@RestController
@RequestMapping("/manage/sms-record/")
public class SmsRecordController extends BaseController {

    @Autowired
    private ITSmsRecordService itSmsRecordService;

    /**
     * 列表
     *
     * @param param
     * @return
     */
    @Log(module = EOperationModule.SMS_CONFIG, business = EOperationBusiness.SMS_RECORD, method = EOperationMethod.SELECT)
    @GetMapping("/list")
    public TableDataInfo list(SmsRecordQueryParam param) {
        startPage();
        List<TSmsRecordExt> list = itSmsRecordService.queryList(param, SecurityUtils.getPkCountry());
        for (TSmsRecordExt tSmsRecordExt : list) {
            tSmsRecordExt.setSendTypeVal(ESmsSendType.getEnumByValue(tSmsRecordExt.getSendType()).getLabel());
            tSmsRecordExt.setSendStatusVal(ESmsSendStatus.getEnumByValue(tSmsRecordExt.getSendStatus()).getLabel());
            tSmsRecordExt.setSendTime(tSmsRecordExt.getModifiedTime());
        }
        return getDataTable(list);
    }

    /**
     * 导出
     *
     * @param response
     * @param param
     */
    @Log(module = EOperationModule.SMS_CONFIG, business = EOperationBusiness.SMS_RECORD, method = EOperationMethod.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SmsRecordQueryParam param) {
        List<TSmsRecordExt> list = itSmsRecordService.queryList(param, SecurityUtils.getPkCountry());
        for (TSmsRecordExt tSmsRecordExt : list) {
            tSmsRecordExt.setSendTypeVal(ESmsSendType.getEnumByValue(tSmsRecordExt.getSendType()).getLabel());
            tSmsRecordExt.setSendStatusVal(ESmsSendStatus.getEnumByValue(tSmsRecordExt.getSendStatus()).getLabel());
            tSmsRecordExt.setSendTime(tSmsRecordExt.getModifiedTime());
        }

        ExcelUtil<TSmsRecordExt> util = new ExcelUtil<>(TSmsRecordExt.class);
        util.exportExcel(response, list, "短信记录导出");
    }

    /**
     * 创建短信
     *
     * @param param
     * @return
     */
    @Log(module = EOperationModule.SMS_CONFIG, business = EOperationBusiness.SMS_CREATE, method = EOperationMethod.INSERT)
    @PostMapping("/create")
    public AjaxResult create(@RequestBody SmsCreateParam param) {
        if (null == param.getTemplateId() || null == param.getSendScope() || null == param.getSendType()) {
            return AjaxResult.error(TransactionUtils.getContent(CommonMsgConstants.MISSING_PARAM));
        }
        if (ESmsSendType.SCHEDULED.getValue() == param.getSendType() && null == param.getSendTime()) {
            // 定时发送，需要有发送时间
            return AjaxResult.error(TransactionUtils.getContent(CommonMsgConstants.MISSING_PARAM));
        }

        String str = itSmsRecordService.pageSms(param, SecurityUtils.getUserId(), SecurityUtils.getPkCountry());
        if (null == str) {
            return AjaxResult.success();
        }
        return AjaxResult.error(str);
    }

}
