package com.bjtdba.userCenter.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.bjtdba.entity.userCenter.UserCertification;
import com.bjtdba.userCenter.dao.UserCertificationDao;
import com.bjtdba.userCenter.service.UserCertificationService;
import com.bjtdba.userCenter.util.IdCardUtil;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;

/**
 * 用户认证(UserCertification)表服务实现类
 *
 * @author makejava
 * @since 2022-10-14 15:40:55
 */
@Service("userCertificationService")
public class UserCertificationServiceImpl implements UserCertificationService {
    @Resource
    private UserCertificationDao userCertificationDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public UserCertification queryById(Integer id) {
        return this.userCertificationDao.queryById(id);
    }

    /**
     * 分页查询
     *
     * @param userCertification 筛选条件
     * @param pageRequest      分页对象
     * @return 查询结果
     */
    @Override
    public Page<UserCertification> queryByPage(UserCertification userCertification, PageRequest pageRequest) {
        long total = this.userCertificationDao.count(userCertification);
        return new PageImpl<>(this.userCertificationDao.queryAllByLimit(userCertification, pageRequest), pageRequest, total);
    }

    /**
     * 新增数据
     *
     * @param userCertification 实例对象
     * @return 实例对象
     */
    @Override
    public UserCertification insert(UserCertification userCertification) {
        this.userCertificationDao.insert(userCertification);
        return userCertification;
    }

    @Override
    public UserCertification queryByUId(Integer uid){
        return userCertificationDao.queryByUId(uid);
    }

    /**
     * 修改数据
     *
     * @param userCertification 实例对象
     * @return 实例对象
     */
    @Override
    public UserCertification update(UserCertification userCertification) {
        this.userCertificationDao.update(userCertification);
        return this.queryById(userCertification.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.userCertificationDao.deleteById(id) > 0;
    }

    public JSONObject add_cer(UserCertification userCertification, Integer uid){
        UserCertification Obj=userCertificationDao.queryByUId(uid);
        int i=0;
        if(Obj!=null&&Obj.getId()>0){
            Obj.setUserCard(userCertification.getUserCard());
            Obj.setUserName(userCertification.getUserName());
            Obj.setCardReverse(userCertification.getCardReverse());
            Obj.setCardPositive(userCertification.getCardPositive());
            Obj.setMobile(userCertification.getMobile());
            Obj.setUpdateTime(new Date().getTime()/1000);
            i=userCertificationDao.update(Obj);
        }else{
            i=userCertificationDao.insert(userCertification);
        }

        JSONObject res=new JSONObject();
        if(i>0){
            res.put("user_name",userCertification.getUserName());
            res.put("age", IdCardUtil.getBirAgeSex(userCertification.getUserCard()));
            res.put("user_card",IdCardUtil.regReplace(userCertification.getUserCard(),"/(\\d{6})\\d{8}(\\d{4})/","$1****$2"));
            return res;

        }else{
            return null;
        }
    }



}
