package com.bonus.zlpt.company.service.impl;

import com.bonus.zlpt.common.security.utils.SecurityUtils;
import com.bonus.zlpt.company.api.domain.BmCompanyInfo;
import com.bonus.zlpt.company.api.domain.dto.BmCompanyDto;
import com.bonus.zlpt.company.api.domain.dto.BmCompanyInfoDto;
import com.bonus.zlpt.company.api.domain.vo.BmCompanyInfoVo;
import com.bonus.zlpt.company.api.domain.vo.BmCompanyVo;
import com.bonus.zlpt.company.mapper.BmCoBankMapper;
import com.bonus.zlpt.company.service.BmCompanyInfoService;
import com.bonus.zlpt.system.api.RemoteUserService;
import com.bonus.zlpt.system.api.domain.SysUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import com.bonus.zlpt.company.mapper.BmCompanyInfoDao;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * Description:
 *
 * @Author 阮世耀
 * @Create 2023/12/1 19:40
 * @Version 1.0
 */

@Service
public class BmCompanyInfoServiceImpl implements BmCompanyInfoService {

    @Resource
    private BmCompanyInfoDao bmCompanyInfoMapper;

    @Autowired
    private BmCoBankMapper bmCoBankMapper;

    @Autowired
    private RemoteUserService remoteUserService;


    @Override
    public int deleteByPrimaryKey(Integer companyId) {
        return bmCompanyInfoMapper.deleteByPrimaryKey(companyId);
    }

    @Override
    public int insert(BmCompanyInfo record) {
        return bmCompanyInfoMapper.insert(record);
    }

    @Override
    @Transactional
    public int insertSelective(BmCompanyInfoDto record) {

        record.getBmCompanyInfo().setCreateTime(LocalDateTime.now().toString());
        record.getBmCompanyInfo().setCreator(SecurityUtils.getUserId().intValue());
        record.getBmCompanyInfo().setStatus("0");
        bmCompanyInfoMapper.insertSelective(record.getBmCompanyInfo());
        record.getBmCoBank().setCoId(record.getBmCompanyInfo().getCompanyId());
        return bmCoBankMapper.insertSelective(record.getBmCoBank());
    }

    @Override
    public BmCompanyInfo selectByPrimaryKey(Integer companyId) {
        return bmCompanyInfoMapper.selectByPrimaryKey(companyId);
    }

    @Override
    @Transactional
    public int updateByPrimaryKeySelective(BmCompanyInfoDto record) {

        bmCoBankMapper.updateByPrimaryKeySelective(record.getBmCoBank());
        return bmCompanyInfoMapper.updateByPrimaryKeySelective(record.getBmCompanyInfo());
    }

    @Override
    public int updateByPrimaryKey(BmCompanyInfo record) {
        return bmCompanyInfoMapper.updateByPrimaryKey(record);
    }

    /**
     * 根据条件查询企业信息(企业入驻审核)
     */
    @Override
    public List<BmCompanyInfo> selectList(BmCompanyDto bmCompanyDto) {

        return bmCompanyInfoMapper.selectList(bmCompanyDto);
    }

    /**
     * 根据 企业id 查询企业信息
     *
     * @param id
     * @return
     */
    @Override
    public BmCompanyVo getCompanyInfoById(Integer id) {

        //根据id查询企业详情
        BmCompanyInfo bmCompanyInfo = bmCompanyInfoMapper.selectByPrimaryKey(id);

        BmCompanyVo bmCompanyVo = new BmCompanyVo();
        bmCompanyVo.setBmCompanyInfo(bmCompanyInfo);

        bmCompanyVo.setBmCoBank(bmCoBankMapper.selectByCompanyId(bmCompanyInfo.getCompanyId()));

        return bmCompanyVo;
    }

    /**
     * 根据条件查询企业信息
     */
    @Override
    public List<BmCompanyInfoVo> selectCompanyInfo(BmCompanyDto bmCompanyDto) {

        List<BmCompanyInfo> list = bmCompanyInfoMapper.selectCompanyInfo(bmCompanyDto);
        List<BmCompanyInfoVo> bmCompanyInfoVos = new ArrayList<>();

        for (BmCompanyInfo bmCompanyInfo : list) {

            Integer userId = bmCompanyInfo.getCreator();
            //获取当前被授权用户信息
            SysUser sysUser = remoteUserService.selectUserById(userId.longValue()).getData();
            int count = selectUserCount(sysUser);
            BmCompanyInfoVo bmCompanyInfoVo = new BmCompanyInfoVo();
            bmCompanyInfoVo.setUserCount(count);
            bmCompanyInfoVo.setBmCompanyInfo(bmCompanyInfo);
            bmCompanyInfoVos.add(bmCompanyInfoVo);
        }

        return bmCompanyInfoVos;
    }


    /**
     * 递归查询用户数
     *
     * @return
     */
    private Integer selectUserCount(SysUser sysUser) {


        List<SysUser> sysUsers = (List<SysUser>) remoteUserService.list(new SysUser()).getRows();
        //有上级，先查顶级
        Long id = sysUser.getUserId();
        if (sysUser.getParentId() != null) {
            id = getRootId(sysUser, sysUsers);
        }
        return getUserCount(1, id, sysUsers);
    }

    /**
     * 从根用户递归获取所有用户
     * @param count
     * @param id
     * @param sysUsers
     * @return
     */
    private Integer getUserCount(int count, Long id, List<SysUser> sysUsers) {


        for (SysUser sysUser : sysUsers) {
            if (sysUser.getParentId().longValue() == id) {
                count++;
                return getUserCount(count, sysUser.getUserId(), sysUsers);
            }
        }
        return count;
    }

    /**
     * 递归获取根用户
     * @param sysUser
     * @param sysUsers
     * @return
     */
    private Long getRootId(SysUser sysUser, List<SysUser> sysUsers) {

        if (sysUser.getParentId() != null) {
            for (SysUser user : sysUsers) {
                if (user.getUserId().intValue() == sysUser.getParentId()) {
                    return getRootId(user, sysUsers);
                }
            }
            return sysUser.getUserId();
        } else {
            return sysUser.getUserId();
        }
    }
}
