package com.muyu.fence.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.muyu.common.core.utils.StringUtils;
import com.muyu.fence.domain.database.ElectronicFenceGroup;
import com.muyu.fence.domain.database.FenceGroupMid;
import com.muyu.fence.domain.req.ElectronicFenceGroupListReq;
import com.muyu.fence.domain.resp.ElectronicFenceGroupResp;
import com.muyu.fence.domain.resp.ElectronicFenceResp;
import com.muyu.fence.mapper.ElectronicFenceGroupMapper;
import com.muyu.fence.service.ElectronicFenceGroupService;
import com.muyu.fence.service.ElectronicFenceService;
import com.muyu.fence.service.FenceGroupMidService;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * @Author：yuping
 * @Package：com.muyu.fence.service.impl
 * @Project：cloud-server
 * @name：FenceGroupServiceImpl
 * @Date：2024/9/18 15:17
 */
@Log4j2
@Service
public class ElectronicFenceGroupServiceImpl extends ServiceImpl<ElectronicFenceGroupMapper, ElectronicFenceGroup> implements ElectronicFenceGroupService {

   @Autowired
   private ElectronicFenceGroupMapper electronicFenceGroupMapper;

   @Autowired
   private ElectronicFenceService electronicFenceService;

   @Autowired
   private FenceGroupMidService fenceGroupMidService;

    @Override
    public List<ElectronicFenceGroup> selectGroupList(ElectronicFenceGroupListReq req) {

        LambdaQueryWrapper<ElectronicFenceGroup> queryWrapper = new LambdaQueryWrapper<>();

        queryWrapper.like(
                StringUtils.isNotNull(req.getGroupName()),ElectronicFenceGroup::getGroupName,req.getGroupName()
        );

        queryWrapper.eq(
                StringUtils.isNotEmpty(req.getGroupType()),ElectronicFenceGroup::getGroupType,req.getGroupType()
        );
        queryWrapper.eq(
                StringUtils.isNotEmpty(req.getStatus()),ElectronicFenceGroup::getStatus, req.getStatus()
        );
        List<ElectronicFenceGroup> list = this.list(queryWrapper);

        return list;

    }

    @Override
    public ElectronicFenceGroupResp findGroupByid(Long id) {

        ElectronicFenceGroup electronicFenceGroup = this.getById(id);

        ElectronicFenceGroupResp electronicFenceGroupResp = ElectronicFenceGroup.buildElectronicFenceGroupResp(electronicFenceGroup);


     List<FenceGroupMid> fenceGroupMidList = fenceGroupMidService.selectFenceGroupMidListById(id);

      if (0<fenceGroupMidList.size()){
          List<Long> ids = fenceGroupMidList.stream().map(FenceGroupMid::getFenceId).toList();

          List<ElectronicFenceResp>  electronicFenceRespList= electronicFenceService.selectListByIds(ids);

          electronicFenceGroupResp.setElectronicFenceRespList(electronicFenceRespList);
      }


        return electronicFenceGroupResp;
    }


}
