package com.yuchen.saas.park.service.service.accredit.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.yuchen.saas.park.api.dto.accredit.ProjectAccreditReqDTO;
import com.yuchen.saas.park.api.entity.park.PmgBatParkInfo;
import com.yuchen.saas.park.api.entity.park.accredit.SaasParkAccreditInfo;
import com.yuchen.saas.park.api.vo.accredit.ProjectAccreditInfoVO;
import com.yuchen.saas.park.service.mapper.accredit.SaasParkAccreditInfoMapper;
import com.yuchen.saas.park.service.service.PmgBatParkInfoService;
import com.yuchen.saas.park.service.service.accredit.ISaasParkAccreditInfoService;
import org.nest.springwrap.core.log.exception.ServiceException;
import org.nest.springwrap.core.mp.base.BaseServiceImpl;
import org.nest.springwrap.core.secure.ContextUser;
import org.nest.springwrap.core.secure.utils.AuthUtil;
import org.nest.springwrap.core.tool.utils.Func;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * <p>
 * 项目授权信息表 服务实现类
 * </p>
 *
 * @author zhouhanwei
 * @since 2024-10-31
 */
@Service
public class SaasParkAccreditInfoServiceImpl extends BaseServiceImpl<SaasParkAccreditInfoMapper, SaasParkAccreditInfo> implements ISaasParkAccreditInfoService {

    @Lazy
    @Resource
    private PmgBatParkInfoService pmgBatParkInfoService;

    @Override
    public Boolean saveProjectAccredit(ProjectAccreditReqDTO reqDTO) {
        ContextUser contextUser = AuthUtil.getUser();
        if (Objects.isNull(contextUser)) {
            throw new ServiceException("用户不存在");
        }
        PmgBatParkInfo pmgBatParkInfo = pmgBatParkInfoService.getById(reqDTO.getProjectId());
        if (Func.isEmpty(pmgBatParkInfo)) {
            throw new ServiceException("项目不存在");
        }
        // 授权请求的客户id列表
        List<Long> addAccreditCustomerIdList = reqDTO.getCustomerIdList();
        // 重新定义新增加的授权客户id列表
        List<Long> newAddAccreditCustomerIdList = new ArrayList<>();
        List<SaasParkAccreditInfo> addListData = new ArrayList<>();
        // 查询已授权列表
        List<SaasParkAccreditInfo> list = list(new LambdaQueryWrapper<SaasParkAccreditInfo>()
                .eq(SaasParkAccreditInfo::getProjectId, reqDTO.getProjectId()));
        if (Func.isNotEmpty(list)) {
            // 删除取消授权的客户，需保留旧授权数据及主添加方
            addAccreditCustomerIdList.add(pmgBatParkInfo.getOperator());
            List<Long> accreditCustomerIdList = list.stream()
                    .map(SaasParkAccreditInfo::getCustomerId)
                    .collect(Collectors.toList());
            // 获取删除取消授权的客户id列表
            List<Long> cancelAccreditCustomerIdList = new ArrayList<>();
            for (Long accreditCustomerId : accreditCustomerIdList) {
                if (!addAccreditCustomerIdList.contains(accreditCustomerId)) {
                    cancelAccreditCustomerIdList.add(accreditCustomerId);
                }
            }
            // 删除取消授权的客户
            if (Func.isNotEmpty(cancelAccreditCustomerIdList)) {
                LambdaUpdateWrapper<SaasParkAccreditInfo> updateWrapper = new LambdaUpdateWrapper<>();
                updateWrapper.in(SaasParkAccreditInfo::getCustomerId, cancelAccreditCustomerIdList);
                remove(updateWrapper);
            }
            // 重新定义新增加的授权客户id列表赋值
            for (Long accreditCustomerId : addAccreditCustomerIdList) {
                if (!accreditCustomerIdList.contains(accreditCustomerId)) {
                    newAddAccreditCustomerIdList.add(accreditCustomerId);
                }
            }

        } else {
            newAddAccreditCustomerIdList = addAccreditCustomerIdList;
        }
        for (Long customerId : newAddAccreditCustomerIdList) {
            // 等于项目添加客户方，过滤掉
            if (customerId.equals(pmgBatParkInfo.getOperator())) {
                continue;
            }
            SaasParkAccreditInfo saasParkAccreditInfo  = new SaasParkAccreditInfo();
            saasParkAccreditInfo.setProjectId(reqDTO.getProjectId());
            saasParkAccreditInfo.setCustomerId(customerId);
            saasParkAccreditInfo.setProjectAddFlag("0");
            saasParkAccreditInfo.setRemark(reqDTO.getRemark());
            saasParkAccreditInfo.setCreateOrgUser(contextUser.getOrgUserId());
            addListData.add(saasParkAccreditInfo);
        }
        return saveBatch(addListData);
    }

    @Override
    public List<ProjectAccreditInfoVO> getProjectAccreditList(Long projectId) {
        List<SaasParkAccreditInfo> list = list(new LambdaQueryWrapper<SaasParkAccreditInfo>()
                .eq(SaasParkAccreditInfo::getProjectId, projectId));
        return BeanUtil.copyToList(list, ProjectAccreditInfoVO.class);
    }

    @Override
    public List<Long> getProjectAccreditCustomerIdList(Long projectId) {
        return list(new LambdaQueryWrapper<SaasParkAccreditInfo>()
                .eq(SaasParkAccreditInfo::getProjectId, projectId))
                .stream().map(SaasParkAccreditInfo::getCustomerId).collect(Collectors.toList());
    }

    @Override
    public List<Long> getProjectAccreditProjectIdList(Long customerId) {
        return list(new LambdaQueryWrapper<SaasParkAccreditInfo>()
                .eq(SaasParkAccreditInfo::getCustomerId, customerId))
                .stream().map(SaasParkAccreditInfo::getProjectId).collect(Collectors.toList());
    }
}
