package com.cusc.nirvana.user.rnr.fp.service.impl;

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.cusc.nirvana.common.result.PageResult;
import com.cusc.nirvana.rds.mybatis.PageHelper;
import com.cusc.nirvana.user.rnr.fp.converter.FpRnrSmsInfoConverter;
import com.cusc.nirvana.user.rnr.fp.dao.FpRnrSmsInfoDao;
import com.cusc.nirvana.user.rnr.fp.dao.entity.FpRnrSmsInfoPO;
import com.cusc.nirvana.user.rnr.fp.dto.FpRnrSmsInfoDTO;
import com.cusc.nirvana.user.rnr.fp.service.IFpRnrSmsInfoService;
import com.cusc.nirvana.user.rnr.mg.constants.CommonDeleteEnum;
import com.cusc.nirvana.user.util.CuscStringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

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

/**
 * 实名业务短信信息(FpRnrSmsInfo)表服务实现类
 *
 * @author yuy336
 * @since 2022-04-19 20:39:07
 */
@Service
public class FpRnrSmsInfoServiceImpl extends ServiceImpl<FpRnrSmsInfoDao, FpRnrSmsInfoPO>
        implements IFpRnrSmsInfoService {

    /**
     * 通过UUID查询单条数据
     *
     * @param bean
     * @return 实例对象
     */
    @Override
    public FpRnrSmsInfoDTO getByUuid(FpRnrSmsInfoDTO bean) {
        FpRnrSmsInfoPO record = this.getPoByUuid(bean.getUuid());
        return FpRnrSmsInfoConverter.INSTANCE.poToDto(record);
    }

    @Override
    public FpRnrSmsInfoDTO getPoByMessageId(FpRnrSmsInfoDTO bean) {
        return FpRnrSmsInfoConverter.INSTANCE.poToDto(getOnetByMessageId(bean.getSendSmsId()));

    }

    /**
     * 通过查询条件查询集合数据
     *
     * @param bean
     * @return 集合对象
     */
    @Override
    public List<FpRnrSmsInfoDTO> queryByList(FpRnrSmsInfoDTO bean) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("is_delete", CommonDeleteEnum.NORMAL.getCode());
        queryWrapper.orderByDesc("create_time");

        queryWrapper.eq(CuscStringUtils.isNotEmpty(bean.getSendSmsId()), "send_sms_id", bean.getSendSmsId());
        queryWrapper.eq(bean.getBizType() != null, "biz_type", bean.getBizType());
        queryWrapper.eq(CuscStringUtils.isNotEmpty(bean.getRnrId()), "rnr_id", bean.getRnrId());

        List<FpRnrSmsInfoPO> record = this.list(queryWrapper);
        return FpRnrSmsInfoConverter.INSTANCE.poListToDtoList(record);
    }

    /**
     * 根据orderid查短信信息
     *
     * @param bean 筛选条件
     * @return 查询结果
     */
    @Override
    public List<FpRnrSmsInfoDTO> queryByOrderId(FpRnrSmsInfoDTO bean) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("order_id",bean.getOrderId());
        queryWrapper.orderByDesc("create_time");
        queryWrapper.eq("is_delete", CommonDeleteEnum.NORMAL.getCode());

        List<FpRnrSmsInfoPO> list = this.list(queryWrapper);
        return FpRnrSmsInfoConverter.INSTANCE.poListToDtoList(list);
    }

    /**
     * 分页查询
     *
     * @param bean 筛选条件
     * @return 查询结果
     */
    @Override
    public PageResult<FpRnrSmsInfoDTO> queryByPage(FpRnrSmsInfoDTO bean) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.orderByDesc("create_time");
        queryWrapper.eq("is_delete", CommonDeleteEnum.NORMAL.getCode());

        Page<FpRnrSmsInfoPO> page =
                this.page(new Page<>(bean.getCurrPage(), bean.getPageSize()), queryWrapper);
        return PageHelper.convert(page, FpRnrSmsInfoDTO.class);
    }

    /**
     * 新增数据
     *
     * @param bean 实例对象
     * @return 实例对象
     */
    @Override
    @Transactional
    public FpRnrSmsInfoDTO insert(FpRnrSmsInfoDTO bean) {
        //发送短信

        //保存记录
        FpRnrSmsInfoPO fpRnrSmsInfoPO = FpRnrSmsInfoConverter.INSTANCE.dtoToPo(bean);
        fpRnrSmsInfoPO.setUuid(CuscStringUtils.generateUuid());
        fpRnrSmsInfoPO.setCreateTime(new Date());
        this.save(fpRnrSmsInfoPO);
        bean.setUuid(fpRnrSmsInfoPO.getUuid());
        return bean;
    }

    /**
     * 修改数据
     *
     * @param bean 实例对象
     * @return 实例对象
     */
    @Override
    @Transactional
    public FpRnrSmsInfoDTO update(FpRnrSmsInfoDTO bean) {
        FpRnrSmsInfoPO fpRnrSmsInfoPO = this.getPoByUuid(bean.getUuid());
        if (fpRnrSmsInfoPO == null) {
            return null;
        }
        FpRnrSmsInfoPO tmpBean = FpRnrSmsInfoConverter.INSTANCE.dtoToPo(bean);
        tmpBean.setId(fpRnrSmsInfoPO.getId());
        this.updateById(tmpBean);
        return bean;
    }

    /**
     * 短信回调后更新短信状态
     *
     * @param bean
     * @return
     */
    @Override
    public FpRnrSmsInfoDTO getPoByPhoneNo(FpRnrSmsInfoDTO bean) {
        QueryWrapper<FpRnrSmsInfoPO> queryWrapper = new QueryWrapper();
        queryWrapper.orderByDesc("create_time");
        queryWrapper.eq("send_phone", bean.getSendPhone());
        queryWrapper.eq("is_delete", 0);
        queryWrapper.eq(!StringUtils.isEmpty(bean.getSendSmsId()),"send_sms_id", bean.getSendSmsId());


        List<FpRnrSmsInfoPO> list = this.list(queryWrapper);

        if (!CollectionUtils.isEmpty(list)) {
            return FpRnrSmsInfoConverter.INSTANCE.poToDto(list.get(0));
        }
        return null;
    }

    /**
     * 通过主键删除数据
     *
     * @param bean 实例对象
     * @return 是否成功
     */
    @Override
    @Transactional
    public boolean deleteById(FpRnrSmsInfoDTO bean) {
        FpRnrSmsInfoPO fpRnrSmsInfoPO = this.getPoByUuid(bean.getUuid());
        if (fpRnrSmsInfoPO == null) {
            return false;
        }
        FpRnrSmsInfoPO tmpBean = new FpRnrSmsInfoPO();
        tmpBean.setId(fpRnrSmsInfoPO.getId());
        tmpBean.setIsDelete(CommonDeleteEnum.DELETED.getCode());
        return this.updateById(tmpBean);
    }

    /**
     * 通过UUID查询单条数据
     *
     * @param uuid
     * @return 实例对象
     */
    private FpRnrSmsInfoPO getPoByUuid(String uuid) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("uuid", uuid);
        queryWrapper.eq("is_delete", CommonDeleteEnum.NORMAL.getCode());
        return this.getOne(queryWrapper);
    }

    /**
     * 通过mssageId查询多条数据
     *
     * @param mssageId
     * @return 实例对象
     */
    private FpRnrSmsInfoPO getOnetByMessageId(String mssageId) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("send_sms_id", mssageId);
        queryWrapper.eq("is_delete", CommonDeleteEnum.NORMAL.getCode());
        return this.getOne(queryWrapper);
    }
}
