package cn.ezhizao.project.business.inspectionChamber.service.impl;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import javax.annotation.Resource;

import cn.ezhizao.project.business.inspectionChamber.domain.BizWorkshopChamber;
import cn.ezhizao.project.business.inspectionChamber.service.IBizWorkshopChamberService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import cn.ezhizao.project.business.inspectionChamber.mapper.BizInspectionChamberMapper;
import cn.ezhizao.project.business.inspectionChamber.domain.BizInspectionChamber;
import cn.ezhizao.project.business.inspectionChamber.service.IBizInspectionChamberService;

/**
 * 检测室配置Service业务层处理
 *
 * @author ezhizao
 * @date 2024-07-17
 */
@Service
public class BizInspectionChamberServiceImpl  extends ServiceImpl<BizInspectionChamberMapper, BizInspectionChamber> implements IBizInspectionChamberService
{
    @Resource
    private BizInspectionChamberMapper bizInspectionChamberMapper;
    @Resource
    private IBizWorkshopChamberService bizWorkshopChamberService;

    /**
     * 查询检测室配置列表
     *
     * @param bizInspectionChamber 检测室配置
     * @return 检测室配置
     */
    @Override
    public List<BizInspectionChamber> getList(BizInspectionChamber bizInspectionChamber)
    {
        return bizInspectionChamberMapper.getList(bizInspectionChamber);
    }

    /**
     * 物理删除
     * @param bizInspectionChamber
     * @return 删除结果
     */
    @Override
    public int physicalDelete(BizInspectionChamber bizInspectionChamber){ return bizInspectionChamberMapper.physicalDelete(bizInspectionChamber); };

    public boolean save(BizInspectionChamber entity, List<Long> workshopIds) {
        boolean saveStatus = super.save(entity);
        List<BizWorkshopChamber> chambers = new ArrayList<>();
        workshopIds.forEach(l -> {
            BizWorkshopChamber item = new BizWorkshopChamber();
            item.setChamberId(entity.getId());
            item.setWorkshopId(l);
            item.setTenantId(entity.getTenantId());
            chambers.add(item);
        });
        QueryWrapper<BizWorkshopChamber> bizWorkshopChamberQueryWrapper = new QueryWrapper<>();
        bizWorkshopChamberQueryWrapper.eq("chamber_id", entity.getId());
        bizWorkshopChamberService.remove(bizWorkshopChamberQueryWrapper);
        saveStatus = saveStatus && (chambers.isEmpty() || bizWorkshopChamberService.saveBatch(chambers));
        return saveStatus;
    }

    public boolean updateById(BizInspectionChamber entity, List<Long> workshopIds) {
        boolean saveStatus = super.updateById(entity);
        List<BizWorkshopChamber> chambers = new ArrayList<>();
        workshopIds.forEach(l -> {
            BizWorkshopChamber item = new BizWorkshopChamber();
            item.setChamberId(entity.getId());
            item.setWorkshopId(l);
            item.setTenantId(entity.getTenantId());
            chambers.add(item);
        });
        QueryWrapper<BizWorkshopChamber> bizWorkshopChamberQueryWrapper = new QueryWrapper<>();
        bizWorkshopChamberQueryWrapper.eq("chamber_id", entity.getId());
        bizWorkshopChamberService.remove(bizWorkshopChamberQueryWrapper);
        saveStatus = saveStatus && (chambers.isEmpty() || bizWorkshopChamberService.saveBatch(chambers));
        return saveStatus;
    }

    @Override
    public boolean removeBatchByIds(Collection<?> list) {
        QueryWrapper<BizWorkshopChamber> bizWorkshopChamberQueryWrapper = new QueryWrapper<>();
        bizWorkshopChamberQueryWrapper.in("chamber_id", list);
        bizWorkshopChamberService.remove(bizWorkshopChamberQueryWrapper);
        return super.removeBatchByIds(list);
    }
}
