package com.njcn.csdevice.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.njcn.csdevice.mapper.CsGroArrMapper;
import com.njcn.csdevice.mapper.CsGroupMapper;
import com.njcn.csdevice.pojo.param.GroupArrParam;
import com.njcn.csdevice.pojo.po.CsGroArr;
import com.njcn.csdevice.pojo.po.CsGroup;
import com.njcn.csdevice.service.ICsGroArrService;
import com.njcn.csdevice.service.ICsGroupService;
import lombok.AllArgsConstructor;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * <p>
 * 组和指标关系表 服务实现类
 * </p>
 *
 * @author xuyang
 * @since 2023-06-19
 */
@Service
@AllArgsConstructor
public class CsGroArrServiceImpl extends ServiceImpl<CsGroArrMapper, CsGroArr> implements ICsGroArrService {

    private final ICsGroupService csGroupService;

    private final CsGroupMapper csGroupMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateGroArr(GroupArrParam groupArrParam) {
        List<GroupArrParam.ArrItem> list = groupArrParam.getData();
        //更新组信息
        if(CollectionUtil.isNotEmpty(list)) {
            List<CsGroup> listGroup = new ArrayList<>();
            List<CsGroArr> arrList = new ArrayList<>();
            LambdaQueryWrapper<CsGroup> csGroupLambdaQueryWrapper = new LambdaQueryWrapper<>();
            csGroupLambdaQueryWrapper.eq(CsGroup::getDataSetId,groupArrParam.getSetId());
            List<CsGroup> csGroupList = csGroupMapper.selectList(csGroupLambdaQueryWrapper);
            List<String> groupIdList = csGroupList.stream().map(CsGroup::getId).collect(Collectors.toList());
            LambdaQueryWrapper<CsGroArr> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            lambdaQueryWrapper.in(CsGroArr::getGroupId, groupIdList);
            this.baseMapper.delete(lambdaQueryWrapper);
            csGroupMapper.delete(csGroupLambdaQueryWrapper);

            Integer sort = 0;
            for (GroupArrParam.ArrItem item : list) {
                CsGroup csGroup = new CsGroup();
                String groupId = IdUtil.simpleUUID();
                if (Objects.isNull(item.getId()) || StringUtils.isBlank(item.getId())){
                    csGroup.setId(groupId);
                } else {
                    csGroup.setId(item.getId());
                }
                csGroup.setDataSetId(groupArrParam.getSetId());
                csGroup.setGroupName(item.getName());
                csGroup.setIsShow(item.getIsShow());
                csGroup.setSort(sort);
                sort++;
                listGroup.add(csGroup);

                if (CollectionUtil.isNotEmpty(item.getChildren())) {
                    for (String idItem : item.getChildren()) {
                        CsGroArr groupArr = new CsGroArr();
                        groupArr.setGroupId(csGroup.getId());
                        groupArr.setArrayId(idItem);
                        arrList.add(groupArr);
                    }
                }
            }
            csGroupService.saveOrUpdateBatch(listGroup,1000);
            this.saveBatch(arrList,1000);
        }
    }

}
