package com.sckw.system.dubbo;

import com.sckw.core.exception.SystemException;
import com.sckw.core.utils.BeanUtils;
import com.sckw.core.utils.CollectionUtils;
import com.sckw.core.web.constant.HttpStatus;
import com.sckw.system.api.RemoteUserService;
import com.sckw.system.api.model.dto.req.ForgetPasswordReqDto;
import com.sckw.system.api.model.dto.req.RegisterReqDto;
import com.sckw.system.api.model.dto.req.UpdatePasswordReqDto;
import com.sckw.system.api.model.dto.req.UserLoginReqDto;
import com.sckw.system.api.model.dto.res.*;
import com.sckw.system.dao.KwsEntTypeDao;
import com.sckw.system.dao.KwsEnterpriseDao;
import com.sckw.system.dao.KwsUserLoginDao;
import com.sckw.system.dao.SysDictDao;
import com.sckw.system.model.*;
import com.sckw.system.model.pojo.FindMenuTreePojo;
import com.sckw.system.model.vo.req.ForgetPasswordReqVo;
import com.sckw.system.model.vo.res.KwsMenuResVo;
import com.sckw.system.service.*;
import jakarta.annotation.Resource;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author czh
 * @desc 远程接口
 * @date 2023/6/12
 */

@DubboService(group = "design", version = "2.0.0")
@Service
public class RemoteUserServiceImpl implements RemoteUserService {

    @Resource
    private RemoteBaseService remoteBaseService;

    @Resource
    private KwsUserService kwsUserService;

    @Resource
    private KwsRoleService kwsRoleService;

    @Resource
    private KwsMenuService kwsMenuService;

    @Resource
    private KwsUserLoginDao kwsUserLoginDao;

    @Resource
    private KwsEnterpriseService kwsEnterpriseService;

    @Resource
    private KwsEnterpriseDao kwsEnterpriseDao;

    @Resource
    private KwsEntTypeDao kwsEntTypeDao;

    @Resource
    private SysDictDao sysDictDao;

    @Resource
    private CommonService commonService;


    @Override
    public KwsEnterpriseResDto queryEnterpriseById(Long id) {
        return remoteBaseService.queryEnterpriseById(id);
    }

    @Override
    public List<KwsRoleResDto> queryRoleInfoByUserId(Long userId) {
        return remoteBaseService.queryRoleInfoByUserId(userId);
    }

    @Override
    public List<UserAccessMenuInfoResDto> queryUserAccessMenu(long id) {
        FindMenuTreePojo findMenuTreePojo = new FindMenuTreePojo();
        List<KwsUserRole> kwsUserRoles = kwsRoleService.findAllByUserId(id);
        if (CollectionUtils.isEmpty(kwsUserRoles)) {
            return Collections.emptyList();
        }
        findMenuTreePojo.setRoleIds(kwsUserRoles.stream().map(KwsUserRole::getRoleId).toList());

        List<KwsMenuResVo> kwsMenuResVos = kwsMenuService.findList(findMenuTreePojo);
        if (CollectionUtils.isEmpty(kwsMenuResVos)) {
            return Collections.emptyList();
        }

        return BeanUtils.copyToList(kwsMenuResVos, UserAccessMenuInfoResDto.class);
    }

    @Override
    public void forgetPassword(ForgetPasswordReqDto reqDto) throws SystemException {
        com.sckw.system.model.vo.req.ForgetPasswordReqVo forgetPasswordReqVo = new ForgetPasswordReqVo();
        BeanUtils.copyProperties(reqDto, forgetPasswordReqVo);
        kwsUserService.forgetPassword(forgetPasswordReqVo);
    }

    @Override
    public void saveUserLogin(UserLoginReqDto userLoginReqDto) {
        KwsUserLogin kwsUserLogin = new KwsUserLogin();
        BeanUtils.copyProperties(userLoginReqDto, kwsUserLogin);
        kwsUserLoginDao.insert(kwsUserLogin);
    }

    @Override
    public UserLoginReqDto currentDayLogin(long userId) {
        KwsUserLogin kwsUserLogin = kwsUserLoginDao.currentDayLogin(userId);
        if (Objects.isNull(kwsUserLogin)) {
            return null;
        }
        UserLoginReqDto userLoginReqDto = new UserLoginReqDto();
        BeanUtils.copyProperties(kwsUserLogin, userLoginReqDto);
        return userLoginReqDto;
    }

    @Override
    public List<EntCertificateResDto> queryCertificateByEntId(Long entId) {
        return remoteBaseService.queryCertificateByEntId(entId);
    }

    @Override
    public List<EntCheckTrackResDto> checkTrackByEntId(Long entId) {
        return remoteBaseService.checkTrackByEntId(entId);
    }


    @Override
    public void updatePassword(UpdatePasswordReqDto updatePassword) {
        kwsUserService.updatePassword(updatePassword);
    }

    @Override
    public void resetPassword(Long id) {
        kwsUserService.resetPassword(id);
    }

    @Override
    public void bindManager(Long entId, Long userId) {
        KwsEnterprise kwsEnterprise = kwsEnterpriseService.queryKwsEnterpriseById(entId);
        if (Objects.isNull(kwsEnterprise)) {
            return;
        }
        kwsEnterprise.setManager(userId);
        if (kwsEnterpriseDao.update(kwsEnterprise) <= 0) {
            throw new SystemException(HttpStatus.CRUD_FAIL_CODE, HttpStatus.INSERT_FAIL);
        }
    }

    @Override
    public Map<Long, SysDictResDto> querySysDictByIds(List<Long> ids) {
        List<SysDict> sysDictList = sysDictDao.selectByKeys(ids);
        if (CollectionUtils.isEmpty(sysDictList)) {
            return new HashMap<>();
        }
        List<SysDictResDto> sysDictResDtos = BeanUtils.copyToList(sysDictList, SysDictResDto.class);
        return sysDictResDtos.stream().collect(Collectors.toMap(SysDictResDto::getId, item -> item, (oldValue, newValue) -> newValue));
    }

    @Override
    public void checkRegisterParam(String entName, String phone, Integer systemType) {
        commonService.checkEntRepeat(entName, phone);
        kwsUserService.checkAccountValid(phone, systemType);
    }

    @Override
    public List<EntTypeResDto> queryEntTypeById(Long entId) {
        return remoteBaseService.queryEntTypeById(entId);
    }

    @Override
    public KwsUserResDto getUserByAccount(String username) {
        return remoteBaseService.getUserByAccount(username);
    }

    @Override
    public List<KwsUserDeptResDto> queryUserDeptByUserId(Long userId) {
        return remoteBaseService.queryUserDeptByUserId(userId);
    }

    @Override
    public List<KwsDeptResDto> queryDeptByIds(List<Long> ids) {
        return remoteBaseService.queryDeptByIds(ids);
    }

    @Override
    public void register(RegisterReqDto reqDto) throws SystemException {
        kwsUserService.register(reqDto);
    }

    @Override
    public void checkPassword(String password, String currentPwd) throws SystemException {
        kwsUserService.checkPassword(password, currentPwd);
    }

    @Override
    public List<KwsUserResDto> checkUserBase(String account, int systemType) throws SystemException {
        KwsUser kwsUser = kwsUserService.checkUserBase(account, systemType);
        KwsUserResDto kwsUserService = new KwsUserResDto();
        BeanUtils.copyProperties(kwsUser, kwsUserService);
        return Collections.singletonList(kwsUserService);
    }

}
