package com.tax.user.service.impl;

import com.tax.user.constants.UserContants;
import com.tax.user.mapper.SysUserTaxnoMapper;
import com.tax.user.model.SysUserTaxno;
import com.tax.user.model.SysUserTaxnoExample;
import com.tax.user.service.api.SysUserTaxnoService;
import com.yxcheng.token.common.model.RuleModel;
import com.yxcheng.token.common.model.UserReq;
import com.yxcheng.token.utils.ObjectUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.function.Predicate;

@Transactional
@Component
public class SysUserTaxnoServiceImpl implements SysUserTaxnoService {

    @Autowired
    private SysUserTaxnoMapper sysUserTaxnoMapper;


    @Override
    public void saveRegisterUser(UserReq userReq) {
        if(!this.isExist(userReq,ObjectUtil::dataBaseListIsExist)){
            SysUserTaxno entity = new SysUserTaxno();
            entity.setTaxno(userReq.getTaxno());
            entity.setTaxName(userReq.getTaxName());
            entity.setUserName(userReq.getUserName());
            entity.setFunctionType(userReq.getApplyFunction());
            entity.setTaxStatus(UserContants.USER_TAX_STATUS_NORMAL);
            entity.setCreateDate(new Date());
            entity.setUpdateDate(new Date());
            entity.setCreateBy(userReq.getAccessKeyID());
            sysUserTaxnoMapper.insert(entity);
        }
    }

    private boolean isExist(UserReq userReq, Predicate<List>predicate) {
        SysUserTaxnoExample example = new SysUserTaxnoExample();
        example.createCriteria().andTaxnoEqualTo(userReq.getTaxno()).andUserNameEqualTo(userReq.getUserName()).andFunctionTypeEqualTo(userReq.getApplyFunction());
        return predicate.test(sysUserTaxnoMapper.selectByExample(example));
    }

    @Override
    public boolean checkUserOwnTaxno(RuleModel model) {
        return sysUserTaxnoMapper.checkUserOwnTaxno(model);
    }

    @Override
    public List<SysUserTaxno> queryUserTaxnoList(String userName) {
        return sysUserTaxnoMapper.queryUserTaxnoList(userName);
    }
}
