package com.casic.missiles.service.Impl.business;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.casic.missiles.mapper.business.BusinessSiteExecutiveInfoEquipmentRelationMapper;
import com.casic.missiles.model.business.BusinessSiteExecutiveEquipmentRelation;
import com.casic.missiles.model.business.BusinessSiteExecutiveInfoEquipmentRelation;
import com.casic.missiles.model.equipment.EquipmentFixedAssets;
import com.casic.missiles.service.business.IBusinessSiteExecutiveInfoEquipmentRelationService;
import com.casic.missiles.service.equipment.IEquipmentFixedAssetsService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author wangpeng
 * @since 2023-04-03
 */
@Service
public class BusinessSiteExecutiveInfoEquipmentRelationServiceImpl extends ServiceImpl<BusinessSiteExecutiveInfoEquipmentRelationMapper, BusinessSiteExecutiveInfoEquipmentRelation> implements IBusinessSiteExecutiveInfoEquipmentRelationService {

    @Resource
    private IEquipmentFixedAssetsService equipmentFixedAssetsService;

    @Override
    public boolean saveBatch(Long siteExecutiveId, List<EquipmentFixedAssets> equipmentInfoList) {
        //先删除所有的绑定关系
        deleteBySiteExecutiveId(siteExecutiveId);
        //新写入绑定关系
        List<BusinessSiteExecutiveInfoEquipmentRelation> list = new ArrayList<>();
        equipmentInfoList.forEach(assets -> {
            BusinessSiteExecutiveInfoEquipmentRelation siteExecutiveInfoEquipmentRelation = new BusinessSiteExecutiveInfoEquipmentRelation();
            siteExecutiveInfoEquipmentRelation.setSiteExecutiveInfoId(siteExecutiveId);
            siteExecutiveInfoEquipmentRelation.setEquipmentId(assets.getId());
            list.add(siteExecutiveInfoEquipmentRelation);
        });
        return this.saveBatch(list);
    }

    @Override
    public Boolean deleteBySiteExecutiveId(Long siteExecutiveId) {
        QueryWrapper<BusinessSiteExecutiveInfoEquipmentRelation> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("site_executive_info_id", siteExecutiveId);
        return this.baseMapper.delete(queryWrapper) > 0 ? true : false;
    }


    @Override
    public Boolean deleteBySiteExecutiveIds(List<Long> siteExecutiveIds) {
        QueryWrapper<BusinessSiteExecutiveInfoEquipmentRelation> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("site_executive_info_id", siteExecutiveIds);
        return this.baseMapper.delete(queryWrapper) > 0 ? true : false;
    }

    @Override
    public List<EquipmentFixedAssets> getEquipmentsByExecutiveInfoId(Long siteExecutiveId) {
        QueryWrapper<BusinessSiteExecutiveInfoEquipmentRelation> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("site_executive_info_id", siteExecutiveId);
        List<BusinessSiteExecutiveInfoEquipmentRelation> list = this.baseMapper.selectList(queryWrapper);
        if (list != null && list.size() > 0) {
            return equipmentFixedAssetsService.list(list.stream().map(BusinessSiteExecutiveInfoEquipmentRelation::getEquipmentId).collect(Collectors.toList()));
        }
        return null;
    }
}
