package com.sckw.system.dubbo;

import com.sckw.core.model.constant.Global;
import com.sckw.core.utils.BeanUtils;
import com.sckw.core.utils.CollectionUtils;
import com.sckw.system.api.model.dto.res.*;
import com.sckw.system.api.model.pojo.DeptInfoPojo;
import com.sckw.system.dao.KwsEntDeptDao;
import com.sckw.system.dao.KwsEntTypeDao;
import com.sckw.system.model.*;
import com.sckw.system.model.pojo.FindEntDeptInfoPojo;
import com.sckw.system.model.vo.res.CertificateResVo;
import com.sckw.system.model.vo.res.KwsDeptResVo;
import com.sckw.system.service.KwsDeptService;
import com.sckw.system.service.KwsEnterpriseService;
import com.sckw.system.service.KwsRoleService;
import com.sckw.system.service.KwsUserService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.Objects;

/**
 * @author czh
 * @desc 提取公共
 * @date 2023/7/7
 */
@Service
public class RemoteBaseService {

    @Resource
    private KwsEnterpriseService kwsEnterpriseService;

    @Resource
    private KwsDeptService kwsDeptService;

    @Resource
    private KwsUserService kwsUserService;

    @Resource
    private KwsRoleService kwsRoleService;

    @Resource
    private KwsEntDeptDao kwsEntDeptDao;

    @Resource
    private KwsEntTypeDao kwsEntTypeDao;


    public KwsEnterpriseResDto queryEnterpriseById(Long id) {
        KwsEnterprise kwsEnterprise = kwsEnterpriseService.queryKwsEnterpriseById(id);
        if (Objects.isNull(kwsEnterprise)) {
            return null;
        }

        KwsEnterpriseResDto kwsEnterpriseResDto = new KwsEnterpriseResDto();
        BeanUtils.copyProperties(kwsEnterprise, kwsEnterpriseResDto);
        return kwsEnterpriseResDto;
    }

    public KwsUserResDto getUserByAccount(String username) {
        return null;
    }

    public List<KwsUserDeptResDto> queryUserDeptByUserId(Long userId) {
        List<KwsUserDept> kwsUserDepts = kwsUserService.queryUserDeptByUserId(userId);
        if (CollectionUtils.isEmpty(kwsUserDepts)) {
            return Collections.emptyList();
        }
        return BeanUtils.copyToList(kwsUserDepts, KwsUserDeptResDto.class);
    }

    public List<KwsDeptResDto> queryDeptByIds(List<Long> ids) {
        List<KwsDept> kwsDepts = kwsUserService.queryDeptByIds(ids);
        if (CollectionUtils.isEmpty(kwsDepts)) {
            return Collections.emptyList();
        }

        return BeanUtils.copyToList(kwsDepts, KwsDeptResDto.class);
    }

    public List<KwsRoleResDto> queryRoleInfoByUserId(Long userId) {
        List<KwsRole> kwsRoles = kwsRoleService.queryRoleByUserId(userId);
        if (CollectionUtils.isEmpty(kwsRoles)) {
            return Collections.emptyList();
        }
        return BeanUtils.copyToList(kwsRoles, KwsRoleResDto.class);
    }

    public List<EntCertificateResDto> queryCertificateByEntId(Long entId) {
        List<CertificateResVo> certificate = kwsEnterpriseService.queryCertificate(entId);
        if (CollectionUtils.isEmpty(certificate)) {
            return Collections.emptyList();
        }
        return BeanUtils.copyToList(certificate, EntCertificateResDto.class);
    }

    public List<EntCheckTrackResDto> checkTrackByEntId(Long entId) {
        List<KwsEntCheckTrack> kwsEntCheckTracks = kwsEnterpriseService.entCheck(entId);
        if (CollectionUtils.isEmpty(kwsEntCheckTracks)) {
            return Collections.emptyList();
        }
        return BeanUtils.copyToList(kwsEntCheckTracks, EntCheckTrackResDto.class);
    }

    public List<DeptInfoPojo> queryDeftInfoByEntId(long entId) {
        KwsDept kwsDept = new KwsDept();
        kwsDept.setEntId(entId);
        kwsDept.setDelFlag(Global.NO);
        List<KwsDeptResVo> list = kwsDeptService.findList(kwsDept);
        if (CollectionUtils.isEmpty(list)) {
            return Collections.emptyList();
        }
        return BeanUtils.copyToList(list, DeptInfoPojo.class);
    }

    public List<KwsEnterprise> queryEntByName(String entName) {
        return kwsEnterpriseService.queryEntByName(entName);
    }


    /**
     * @param entCacheResDto 返参
     * @desc: 填充父子属id
     * @author: czh
     * @date: 2023/7/17
     */
    public void fillRelate(EntCacheResDto entCacheResDto) {
        Long id = entCacheResDto.getId();
        KwsEntDept kwsEntDept = kwsEntDeptDao.selectByEntId(id);
        //不为空则是主体单位
        if (Objects.nonNull(kwsEntDept)) {
            entCacheResDto.setParentId(kwsEntDept.getEntPid());
            return;
        }

        List<KwsEntDept> kwsEntDepts = kwsEntDeptDao.selectByEntPid(id);
        if (CollectionUtils.isNotEmpty(kwsEntDepts)) {
            entCacheResDto.setEntDeptIds(String.join(Global.COMMA, kwsEntDepts.stream().map(KwsEntDept::getEntId).map(String::valueOf).toList()));
        }
    }


    public List<KwsEntDept> queryEntDeptCacheByIds(List<Long> entIds) {
        return kwsEntDeptDao.selectByEntPidList(entIds);
    }

    public List<EntTypeResDto> queryEntTypeById(Long entId) {
        List<KwsEntType> list = kwsEntTypeDao.findListByEntId(entId);
        if (CollectionUtils.isEmpty(list)) {
            return Collections.emptyList();
        }

        return BeanUtils.copyToList(list, EntTypeResDto.class);
    }

    /**
     * 根据用户姓名模糊查用户信息
     * @param name 用户姓名
     * @return list
     */
    public List<KwsUserResDto> getUserByName(String name) {
        return kwsUserService.getUserByName(name);
    }
}
