/*
 * Author: wangjian
 * 生成ServiceImpl
 */
package com.tofly.feesapi.sms.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tofly.feesapi.common.core.ResultRespone;
import com.tofly.feesapi.sms.entity.SmsTxlx;
import com.tofly.feesapi.sms.entity.dto.SmsTxlxDto;
import com.tofly.feesapi.sms.entity.vo.SmsTxlxVo;
import com.tofly.feesapi.sms.service.ISmsTxlxService;
import org.springframework.stereotype.Service;
import com.tofly.feesapi.sms.mapper.SmsTxlxMapper;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * 短信模板
 *
 * @author sunjian
 * @date Fri Nov 08 00:00:00 CST 2024
 */
@Service("smsTxlxService")
public class SmsTxlxServiceImpl extends ServiceImpl<SmsTxlxMapper, SmsTxlx> implements ISmsTxlxService {

    @Transactional(rollbackFor = Exception.class)
    @Override
    public ResultRespone<Object> saveMessage(SmsTxlx smsTxlx) {
        ResultRespone<Object> rest = new ResultRespone<>();
        SmsTxlx txlx = this.getOne(new QueryWrapper<SmsTxlx>().select(" NVL(MAX(code),0)+1 as code"));
        if (StringUtils.isEmpty(txlx)) {
            smsTxlx.setCode("1");
        }
        smsTxlx.setCode(txlx.getCode());
        if (this.save(smsTxlx)) {
            rest.setCode(1);
            rest.setResult("成功");
        } else {
            rest.setCode(-1);
            rest.setResult("失败");
        }
        return rest;
    }

    /**
     * 自定义分页查询
     */
    @Override
    public Page<SmsTxlxVo> getSmsTxlxPage(Page page, SmsTxlxDto dto) {
        SmsTxlx smsTxlx = JSONObject.parseObject(JSON.toJSONString(dto), SmsTxlx.class);
        QueryWrapper<SmsTxlx> queryWrapper = new QueryWrapper<>();
        String searchText = dto.getSearchText();

        //关键字查询
        if (null != searchText) {
            queryWrapper.or().like("code", searchText);
            queryWrapper.or().like("name", searchText);
            queryWrapper.or().like("dxnrgs", searchText);
        }


        queryWrapper.setEntity(smsTxlx);
        queryWrapper.orderByDesc("code");

        Page infoPageList = page(page, queryWrapper);
        infoPageList.setRecords(viewConversion(infoPageList.getRecords()));
        return infoPageList;
    }

    /**
     * 转换视图
     */
    public List<SmsTxlxVo> viewConversion(List<SmsTxlx> infoList) {
        if (CollectionUtils.isEmpty(infoList)) {
            return Collections.emptyList();
        }

        List<SmsTxlxVo> infoVoList = new ArrayList<>();
        infoList.forEach(info -> {
            SmsTxlxVo baseInfo = JSONObject.parseObject(JSON.toJSONString(info), SmsTxlxVo.class);

            if (null != info.getSffsbz()) {
                baseInfo.setSffsbzName(info.getSffsbz().equals("1") ? "自定义格式" : "固定格式");
            }
            infoVoList.add(baseInfo);
        });
        return infoVoList;
    }
}
