package com.willalp.system.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.willalp.common.core.domain.entity.SysUser;
import com.willalp.common.exception.base.BaseException;
import com.willalp.common.utils.DateUtils;
import com.willalp.common.utils.SecurityUtils;
import com.willalp.number.service.IHsSysNumberRuleService;
import com.willalp.system.domain.SysCardInfo;
import com.willalp.system.mapper.SysCardInfoMapper;
import com.willalp.system.mapper.SysUserMapper;
import com.willalp.system.mapper.SysUserOrganizationMapper;
import com.willalp.system.service.ISysCardInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 卡片用户信息Service业务层处理
 *
 * @author willalp
 * @date 2021-11-30
 */
@Service
public class SysCardInfoServiceImpl extends ServiceImpl<SysCardInfoMapper, SysCardInfo> implements ISysCardInfoService
{
    @Autowired
    private SysCardInfoMapper sysCardInfoMapper;

    @Autowired
    private SysUserMapper sysUserMapper;

    @Autowired
    private IHsSysNumberRuleService iHsSysNumberRuleService;

    @Autowired
    private SysUserOrganizationMapper sysUserOrganizationMapper;

    /**
     * 查询卡片用户信息列表
     *
     * @param sysCardInfo 卡片用户信息
     * @return 卡片用户信息
     */
    @Override
    public List<SysCardInfo> selectSysCardInfoList(SysCardInfo sysCardInfo) {
        return sysCardInfoMapper.selectSysCardInfoList(sysCardInfo);
    }

    @Override
    public void saveInfo(SysCardInfo sysCardInfo) {
//        if(StrUtil.isNotBlank(sysCardInfo.getUserId())) {
//            SysUserOrganization org = new SysUserOrganization();
//            org.setLoginName(SecurityUtils.getUsername());
//            List<SysUserOrganization> list = sysUserOrganizationMapper.selectSysUserOrganizationList(org);
//            if (list.isEmpty()) {
//                throw new BaseException("system.user.errorCode2", new String[]{
//                        sysCardInfo.getUserId()
//                });
//            }
//            list = list.stream().filter(e -> e.getIsDefault() != null && e.getIsDefault() == 1).collect(Collectors.toList());
//            if(!list.isEmpty()) {
//                sysCardInfo.setOrganizationCode(list.get(0).getOrganizationCode());
//            }
//        }
        SysCardInfo exist = new SysCardInfo();
        exist.setUserId(sysCardInfo.getUserId());
        exist.setOrganizationCode(sysCardInfo.getOrganizationCode());
        exist.setCardState(0);
        List<SysCardInfo> exists = sysCardInfoMapper.selectSysCardInfoList(exist);
        if (CollectionUtil.isNotEmpty(exists)){
            throw new BaseException("system.card.errorCode2",new Object[]{});
        }
        while (true) {
            String idStr = IdWorker.getIdStr();
            SysCardInfo byId = sysCardInfoMapper.selectById(idStr);
            if (null == byId) {
                sysCardInfo.setId(idStr);
                break;
            }
        }
        sysCardInfo.setIsDelete(0);
        sysCardInfo.setCreateBy(SecurityUtils.getUsername());
        sysCardInfo.setCreateTime(DateUtils.getNowDate());
//        NumberRuleReq req = new NumberRuleReq();
//        req.setRuleId(NumberRuleEnum.CARD.getValue());
//        String code = iHsSysNumberRuleService.getNumber(req);
//        if(StrUtil.isBlank(code)) {
//            throw new BaseException("system.card.errorCode1", new Object[]{});
//        }
//        sysCardInfo.setCardCode(code);
        sysCardInfoMapper.insert(sysCardInfo);
    }

    @Override
    public void updateInfoById(SysCardInfo sysCardInfo) {
        if(StrUtil.isNotBlank(sysCardInfo.getUserId())) {
            SysUser user = sysUserMapper.selectUserByUserName(sysCardInfo.getUserId());
            if (user == null) {
                throw new BaseException("system.user.errorCode2", new String[]{
                        sysCardInfo.getUserId()
                });
            }
            sysCardInfo.setOrganizationCode(user.getOrganizationCode());
        }
        sysCardInfo.setUpdateBy(SecurityUtils.getUsername());
        sysCardInfo.setUpdateTime(DateUtils.getNowDate());
        sysCardInfoMapper.updateById(sysCardInfo);
    }

}
