package com.willalp.system.service.impl;

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.SysDevice;
import com.willalp.system.domain.SysUserOrganization;
import com.willalp.system.mapper.SysDeviceMapper;
import com.willalp.system.mapper.SysUserMapper;
import com.willalp.system.mapper.SysUserOrganizationMapper;
import com.willalp.system.service.ISysDeviceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 设备账户Service业务层处理
 *
 * @author willalp
 * @date 2021-12-01
 */
@Service
public class SysDeviceServiceImpl extends ServiceImpl<SysDeviceMapper, SysDevice>  implements ISysDeviceService {
    @Autowired
    private SysDeviceMapper sysDeviceMapper;

    @Autowired
    private SysUserMapper sysUserMapper;

    @Autowired
    private IHsSysNumberRuleService iHsSysNumberRuleService;

    @Autowired
    private SysUserOrganizationMapper sysUserOrganizationMapper;

    /**
     * 查询设备账户列表
     *
     * @param sysDevice 设备账户
     * @return 设备账户
     */
    @Override
    public List<SysDevice> selectSysDeviceList(SysDevice sysDevice) {
        return sysDeviceMapper.selectSysDeviceList(sysDevice);
    }

    @Override
    public void saveInfo(SysDevice sysDevice) {
        if(StrUtil.isNotBlank(sysDevice.getUserId())) {
            List<SysUserOrganization> list = sysUserOrganizationMapper.queryOrganizationByLoginName(SecurityUtils.getUsername());
            if (list.isEmpty()) {
                throw new BaseException("system.user.errorCode2", new String[]{
                        sysDevice.getUserId()
                });
            }
            list = list.stream().filter(e -> e.getIsDefault() != null && e.getIsDefault() == 1).collect(Collectors.toList());
            if(!list.isEmpty()) {
                sysDevice.setUserName(list.get(0).getNickName());
                sysDevice.setOrganizationCode(list.get(0).getOrganizationCode());
            }
        }
        while (true) {
            String idStr = IdWorker.getIdStr();
            SysDevice byId = sysDeviceMapper.selectById(idStr);
            if (null == byId) {
                sysDevice.setId(idStr);
                break;
            }
        }
        sysDevice.setIsDelete(0);
        sysDevice.setCreateBy(SecurityUtils.getUsername());
        sysDevice.setCreateTime(DateUtils.getNowDate());
//        NumberRuleReq req = new NumberRuleReq();
//        req.setRuleId(NumberRuleEnum.DEVICE.getValue());
//        String code = iHsSysNumberRuleService.getNumber(req);
//        if(StrUtil.isBlank(code)) {
//            throw new BaseException("system.device.errorCode1", new Object[]{});
//        }
//        sysDevice.setDeviceId(code);
        sysDeviceMapper.insert(sysDevice);
    }

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


}
