package com.persagy.dmp.rwd.digital.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.persagy.dmp.common.constant.CommonConstant;
import com.persagy.dmp.common.context.AppContext;
import com.persagy.dmp.common.model.response.CommonResult;
import com.persagy.dmp.common.utils.ResultHelper;
import com.persagy.dmp.digital.entity.ObjectDigital;
import com.persagy.dmp.rwd.basic.constant.DigitalObjectType;
import com.persagy.dmp.rwd.digital.dao.ObjectDigitalMapper;
import com.persagy.dmp.rwd.digital.service.ProjectBusinessService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

@Service
@Slf4j
@RequiredArgsConstructor
public class ProjectBusinessServiceImpl implements ProjectBusinessService {

    private final ObjectDigitalMapper objectDigitalMapper;

    /***
     * Description: 根据项目id查询项目信息
     * @param projectIds : 项目id集合
     * @return : com.persagy.dmp.common.model.response.CommonResult<java.util.List<com.persagy.dmp.rwd.digital.entity.ObjectDigital>>
     * @author : lijie
     * @date :2021/8/18 15:56
     * Update By lijie 2021/8/18 15:56
     */
    @Override
    public CommonResult<List<ObjectDigital>> batchQueryProjectByIds(List<String> projectIds) {
        // 1.组装查询条件
        LambdaQueryWrapper<ObjectDigital> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ObjectDigital::getGroupCode, AppContext.getContext().getGroupCode());
        queryWrapper.eq(ObjectDigital::getValid,true);
        if (CollUtil.isNotEmpty(projectIds)){
            queryWrapper.in(ObjectDigital::getId,projectIds);
        }
        queryWrapper.eq(ObjectDigital::getObjType, DigitalObjectType.PROJECT.getIndex());
        // 2.查询项目信息
        List<ObjectDigital> objectDigitals = objectDigitalMapper.selectList(queryWrapper);
        if (CollUtil.isEmpty(objectDigitals)){
            return ResultHelper.success();
        }
        return ResultHelper.multi(objectDigitals);
    }
    /***
     * Description: 根据管理分区id批量查询项目信息
     * @param groupManageZoneIds : 管理分区id集合
     * @return : com.persagy.dmp.common.model.response.CommonResult<java.util.List<com.persagy.dmp.rwd.digital.entity.ObjectDigital>>
     * @author : lijie
     * @date :2021/8/19 14:09
     * Update By lijie 2021/8/19 14:09
     */
    @Override
    public CommonResult<List<ObjectDigital>> batchQueryProjectByGroupManageZoneIds(Set<String> groupManageZoneIds) {
        QueryWrapper<ObjectDigital> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(StrUtil.toUnderlineCase(CommonConstant.QUERY_GROUPCODE), AppContext.getContext().getGroupCode());
        queryWrapper.eq(ObjectDigital.PROP_VALID,true);
        queryWrapper.eq(ObjectDigital.OBJ_TYPE, DigitalObjectType.PROJECT.getIndex());
        if (CollUtil.isNotEmpty(groupManageZoneIds)){
            queryWrapper.in("JSON_VALUE(infos,'$.groupManageZone')",groupManageZoneIds);
        }
        // 2.查询项目信息
        List<ObjectDigital> objectDigitals = objectDigitalMapper.selectList(queryWrapper);
        if (CollUtil.isEmpty(objectDigitals)){
            return ResultHelper.success();
        }
        return ResultHelper.multi(objectDigitals);
    }

}
