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

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cusc.nirvana.user.rnr.fp.converter.FpRnrRelationInfoConverter;
import com.cusc.nirvana.user.rnr.fp.dao.FpRnrRelationInfoDao;
import com.cusc.nirvana.user.rnr.fp.dao.entity.FpRnrRelationInfoPO;
import com.cusc.nirvana.user.rnr.fp.dto.BindUserInfoDTO;
import com.cusc.nirvana.user.rnr.fp.dto.FpRnrRelationInfoDTO;
import com.cusc.nirvana.user.rnr.fp.service.IFpRnrRelationInfoService;
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 java.util.List;

/**
 * 实名关联信息(FpRnrRelationInfo)表服务实现类
 *
 * @author yuy336
 * @since 2022-04-14 20:43:07
 */
@Service
public class FpRnrRelationInfoServiceImpl extends ServiceImpl<FpRnrRelationInfoDao, FpRnrRelationInfoPO> implements IFpRnrRelationInfoService {

    /**
     * 通过UUID查询单条数据
     *
     * @param bean
     * @return 实例对象
     */
    @Override
    public FpRnrRelationInfoDTO getByUuid(FpRnrRelationInfoDTO bean) {
        FpRnrRelationInfoPO record = this.getPoByUuid(bean.getUuid());;
        return FpRnrRelationInfoConverter.INSTANCE.poToDto(record);
    }
    
     /**
     * 通过查询条件查询集合数据
     *
     * @param bean
     * @return 集合对象
     */
    @Override
    public List<FpRnrRelationInfoDTO> queryByList(FpRnrRelationInfoDTO bean) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("is_delete", CommonDeleteEnum.NORMAL.getCode());
        queryWrapper.orderByDesc("create_time");
        List<FpRnrRelationInfoPO> record = this.list(queryWrapper);
        return FpRnrRelationInfoConverter.INSTANCE.poListToDtoList(record);
    }

    /**
     * 新增数据
     *
     * @param bean 实例对象
     * @return 实例对象
     */
    @Override
    @Transactional
    public FpRnrRelationInfoDTO insert(FpRnrRelationInfoDTO bean) {
       FpRnrRelationInfoPO fpRnrRelationInfoPO = FpRnrRelationInfoConverter.INSTANCE.dtoToPo(bean);
	   fpRnrRelationInfoPO.setUuid(CuscStringUtils.generateUuid());
        this.save(fpRnrRelationInfoPO);
		bean.setUuid(fpRnrRelationInfoPO.getUuid());
        return bean;
    }

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

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

    //查询绑定状态
    @Override
    public List<FpRnrRelationInfoDTO> queryByUserId(FpRnrRelationInfoDTO bean) {
        List<FpRnrRelationInfoPO> record = this.getPoByUserId(bean.getUserId());
        return FpRnrRelationInfoConverter.INSTANCE.poListToDtoList(record);
    }

    //绑定实名
    @Override
    public Boolean bindByUserId(BindUserInfoDTO bean){
        //todo 绑定实名信息
        return true;
    }

    //解绑
    @Override
    public Boolean disableBindByUserId(FpRnrRelationInfoDTO bean){
        bean.setUserId(null);
        FpRnrRelationInfoDTO insert = update(bean);
        if(insert != null){
            return true;
        }else {
            return false;
        }
    }
    
    /**
     * 通过UUID查询单条数据
     *
     * @param uuid
     * @return 实例对象
     */
    private FpRnrRelationInfoPO getPoByUuid(String uuid) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("uuid", uuid);
        queryWrapper.eq("is_delete", CommonDeleteEnum.NORMAL.getCode());
        return this.getOne(queryWrapper);
    }

    private List<FpRnrRelationInfoPO> getPoByUserId(String userId) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("user_id", userId);
        queryWrapper.eq("is_delete", CommonDeleteEnum.NORMAL.getCode());
        queryWrapper.orderByDesc("create_time");
        return this.list(queryWrapper);
    }


}
