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

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hzs.common.core.annotation.Log;
import com.hzs.common.core.constant.SmsConstants;
import com.hzs.common.core.constant.SystemFieldConstants;
import com.hzs.common.core.constant.msg.CommonMsgConstants;
import com.hzs.common.core.enums.EOperationBusiness;
import com.hzs.common.core.enums.EOperationMethod;
import com.hzs.common.core.enums.EOperationModule;
import com.hzs.common.core.enums.EYesNo;
import com.hzs.common.core.utils.DateUtils;
import com.hzs.common.core.utils.StringUtils;
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.TSmsTemplate;
import com.hzs.common.security.utils.SecurityUtils;
import com.hzs.common.util.TransactionUtils;
import com.hzs.third.sms.param.SmsTemplateQueryParam;
import com.hzs.third.sms.service.ITSmsTemplateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * @Description: 短信模板
 * @Author: jiang chao
 * @Time: 2023/3/8 10:10
 * @Classname: SmsRecordController
 * @PackageName: com.hzs.third.sms.controller
 */
@RestController
@RequestMapping("/manage/sms-template/")
public class SmsTemplateController extends BaseController {

    @Autowired
    private ITSmsTemplateService iTSmsTemplateService;

    /**
     * 列表
     *
     * @param param
     * @return
     */
    @Log(module = EOperationModule.SMS_CONFIG, business = EOperationBusiness.SMS_TEMPLATE, method = EOperationMethod.SELECT)
    @GetMapping("/list")
    public TableDataInfo list(SmsTemplateQueryParam param) {
        QueryWrapper<TSmsTemplate> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotEmpty(param.getTemplateName())) {
            queryWrapper.likeRight("TEMPLATE_NAME", param.getTemplateName());
        }
        if (null != param.getEnable()) {
            queryWrapper.eq("ENABLE", param.getEnable());
        }
        if (null != param.getCreationTime() && param.getCreationTime().length > 0) {
            DateUtils.handTimeStartAndEnd(param.getCreationTime());
            queryWrapper.ge(SystemFieldConstants.CREATION_TIME, param.getCreationTime()[0]);
            queryWrapper.le(SystemFieldConstants.CREATION_TIME, param.getCreationTime()[1]);
        }
        queryWrapper.orderByDesc(SystemFieldConstants.CREATION_TIME).orderByAsc("TEMPLATE_NAME");

        startPage();
        List<TSmsTemplate> list = iTSmsTemplateService.list(queryWrapper);
        return getDataTable(list);
    }

    /**
     * 详情
     *
     * @param pkId
     * @return
     */
    @GetMapping("/detail/{pkId}")
    public AjaxResult detail(@PathVariable("pkId") Integer pkId) {
        TSmsTemplate tSmsTemplate = iTSmsTemplateService.getById(pkId);
        if (null == tSmsTemplate) {
            return AjaxResult.error("短信模板不存在");
        }
        return AjaxResult.success(tSmsTemplate);
    }

    /**
     * 保存/更新短信模板
     *
     * @param param
     * @return
     */
    @Log(module = EOperationModule.SMS_CONFIG, business = EOperationBusiness.SMS_TEMPLATE, method = EOperationMethod.UPDATE)
    @PostMapping("/save")
    public AjaxResult save(@RequestBody TSmsTemplate param) {
        if (StringUtils.isAnyEmpty(param.getTemplateName(), param.getTemplateContent(), param.getTemplateCode())) {
            return AjaxResult.error(TransactionUtils.getContent(CommonMsgConstants.MISSING_PARAM));
        }
        Integer pkCountry = SecurityUtils.getPkCountry();

        // 计算模板内容中占位数量就是参数数量
        param.setParamNum(StringUtils.countMatches(param.getTemplateContent(), SmsConstants.SMS_PLACEHOLDER));

        // 校验编码是否重复
        if (!iTSmsTemplateService.checkTemplateCode(param.getTemplateCode(), pkCountry, param.getPkId())) {
            return AjaxResult.error("短信编号已存在");
        }

        if (null != param.getPkId()) {
            // 更新
            param.setPkModified(SecurityUtils.getUserId());
            param.setModifiedTime(new Date());
        } else {
            // 新增
            param.setPkCreator(SecurityUtils.getUserId());
            param.setCreationTime(new Date());
            param.setPkCountry(pkCountry);
        }

        if (iTSmsTemplateService.saveOrUpdate(param)) {
            return AjaxResult.success();
        }
        return AjaxResult.error();
    }

    /**
     * 修改状态
     *
     * @param smsTemplate
     * @return
     */
    @Log(module = EOperationModule.SMS_CONFIG, business = EOperationBusiness.SMS_TEMPLATE, method = EOperationMethod.UPDATE_STATUS)
    @PutMapping("/change-enable")
    public AjaxResult changeEnable(@RequestBody TSmsTemplate smsTemplate) {
        if (null == smsTemplate.getPkId() || null == smsTemplate.getEnable()) {
            return AjaxResult.error(TransactionUtils.getContent(CommonMsgConstants.MISSING_PARAM));
        }

        if (null == iTSmsTemplateService.getById(smsTemplate.getPkId())) {
            return AjaxResult.error("短信模板不在");
        }

        TSmsTemplate tSmsTemplate = new TSmsTemplate();
        tSmsTemplate.setPkId(smsTemplate.getPkId());
        tSmsTemplate.setEnable(smsTemplate.getEnable());
        tSmsTemplate.setPkModified(SecurityUtils.getUserId());
        tSmsTemplate.setModifiedTime(new Date());
        iTSmsTemplateService.updateById(tSmsTemplate);

        return AjaxResult.success();
    }


    /**
     * 列表
     *
     * @param param
     * @return
     */
    @GetMapping("/all")
    public AjaxResult all(SmsTemplateQueryParam param) {
        QueryWrapper<TSmsTemplate> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotEmpty(param.getTemplateName())) {
            queryWrapper.likeRight("TEMPLATE_NAME", param.getTemplateName());
        }
        queryWrapper.eq("ENABLE", EYesNo.YES.getIntValue());
        queryWrapper.orderByDesc(SystemFieldConstants.CREATION_TIME);

        return AjaxResult.success(iTSmsTemplateService.list(queryWrapper));
    }

}
