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.BusinessSiteExecutiveEquipmentRelationMapper;
import com.casic.missiles.model.business.BusinessSiteExecutiveEquipmentRelation;
import com.casic.missiles.service.business.IBusinessSiteExecutiveEquipmentRelationService;
import com.casic.missiles.service.equipment.IEquipmentFixedAssetsService;
import org.springframework.stereotype.Service;

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

/**
 * <p>
 * 外场检测-测量设备关联表 服务实现类
 * </p>
 *
 * @author wangpeng
 * @since 2023-03-23
 */
@Service
public class BusinessSiteExecutiveEquipmentRelationServiceImpl extends ServiceImpl<BusinessSiteExecutiveEquipmentRelationMapper, BusinessSiteExecutiveEquipmentRelation> implements IBusinessSiteExecutiveEquipmentRelationService {


    @Resource
    private IEquipmentFixedAssetsService equipmentFixedAssetsService;

    @Override
    public boolean saveBatch(Long siteExecutiveLogId, List<BusinessSiteExecutiveEquipmentRelation> equipmentInfoList) {
        //先删除所有的绑定关系
        deleteBySiteExecutiveLogId(siteExecutiveLogId);
        //新写入绑定关系
        List<BusinessSiteExecutiveEquipmentRelation> list = new ArrayList<>();
        equipmentInfoList.forEach(assets -> {
            BusinessSiteExecutiveEquipmentRelation siteExecutiveEquipmentRelation = new BusinessSiteExecutiveEquipmentRelation();
            siteExecutiveEquipmentRelation.setSiteExecutiveLogId(siteExecutiveLogId);
            siteExecutiveEquipmentRelation.setSiteExecutiveId(assets.getSiteExecutiveId());
            siteExecutiveEquipmentRelation.setEquipmentNo(assets.getEquipmentNo());
            siteExecutiveEquipmentRelation.setEquipmentId(assets.getEquipmentId());
            siteExecutiveEquipmentRelation.setEquipmentModel(assets.getEquipmentModel());
            siteExecutiveEquipmentRelation.setBeforeWorkCheck(assets.getBeforeWorkCheck());
            siteExecutiveEquipmentRelation.setBeforeWorkRecorderDate(assets.getBeforeWorkRecorderDate());
            siteExecutiveEquipmentRelation.setBeforeWorkRecorderId(assets.getBeforeWorkRecorderId());
            siteExecutiveEquipmentRelation.setAfterWorkCheck(assets.getAfterWorkCheck());
            siteExecutiveEquipmentRelation.setAfterWorkRecorderDate(assets.getAfterWorkRecorderDate());
            siteExecutiveEquipmentRelation.setAfterWorkRecorderId(assets.getAfterWorkRecorderId());
            siteExecutiveEquipmentRelation.setBeforeWorkResult(assets.getBeforeWorkResult());
            siteExecutiveEquipmentRelation.setAfterWorkResult(assets.getAfterWorkResult());
            list.add(siteExecutiveEquipmentRelation);
        });
        return this.saveBatch(list);
    }

    @Override
    public Boolean deleteBySiteExecutiveLogId(Long siteExecutiveLogId) {
        QueryWrapper<BusinessSiteExecutiveEquipmentRelation> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("site_executive_log_id", siteExecutiveLogId);
        return this.baseMapper.delete(queryWrapper) > 0 ? true : false;
    }


    @Override
    public Boolean deleteBySiteExecutiveLogIds(List<Long> siteExecutiveLogIds) {
        QueryWrapper<BusinessSiteExecutiveEquipmentRelation> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("site_executive_log_id", siteExecutiveLogIds);
        return this.baseMapper.delete(queryWrapper) > 0 ? true : false;
    }

    @Override
    public List<BusinessSiteExecutiveEquipmentRelation> getEquipmentsByExecutiveLogId(Long siteExecutiveLogId) {
        QueryWrapper<BusinessSiteExecutiveEquipmentRelation> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("site_executive_log_id", siteExecutiveLogId);
        return this.baseMapper.selectList(queryWrapper);
    }
}
