package com.muyu.enterprise.service.impl;

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.enterprise.domain.dateBase.ElectronicFenceGroup;
import com.muyu.enterprise.domain.dateBase.FenceGroupMid;
import com.muyu.enterprise.domain.req.ElectronicFenceGroupListReq;
import com.muyu.enterprise.domain.resp.ElectronicFenceGroupResp;
import com.muyu.enterprise.domain.resp.ElectronicFenceResp;
import com.muyu.enterprise.mapper.ElectronicFenceGroupMapper;
import com.muyu.enterprise.service.ElectronicFenceGroupService;
import com.muyu.enterprise.service.ElectronicFenceService;
import com.muyu.enterprise.service.FenceGroupMidService;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author：李庆帅
 * @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.isNotNull(req.getPriority()),ElectronicFenceGroup::getPriority,req.getPriority()
        );
        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;
    }


}
