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

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.casic.missiles.dto.IdDTO;
import com.casic.missiles.dto.ReturnDTO;
import com.casic.missiles.dto.ReturnUtil;
import com.casic.missiles.enums.BusinessExceptionEnum;
import com.casic.missiles.exception.BusinessException;
import com.casic.missiles.mapper.meter.MeterFileMapper;
import com.casic.missiles.mapper.equipment.EquipmentStandardVerifyRegulationMapper;
import com.casic.missiles.model.meter.MeterFile;
import com.casic.missiles.model.equipment.EquipmentStandardEquipment;
import com.casic.missiles.model.equipment.EquipmentStandardVerifyRegulation;
import com.casic.missiles.service.equipment.IEquipmentStandardEquipmentService;
import com.casic.missiles.service.equipment.IEquipmentStandardVerifyRegulationService;
import com.casic.missiles.utils.DictCodeUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 标准装置-检定规程表 服务实现类
 * </p>
 *
 * @author cuicheng
 * @since 2022-12-15
 */
@Service
public class EquipmentStandardVerifyRegulationServiceImpl extends ServiceImpl<EquipmentStandardVerifyRegulationMapper, EquipmentStandardVerifyRegulation> implements IEquipmentStandardVerifyRegulationService {

    @Autowired
    private EquipmentStandardVerifyRegulationMapper meterStandardVerifyRegulationMapper;

    @Autowired
    private MeterFileMapper meterFileMapper;

    @Autowired
    private IEquipmentStandardEquipmentService meterStandardEquipmentService;

    @Override
    public Page<EquipmentStandardVerifyRegulation> listPage(Page<EquipmentStandardVerifyRegulation> page, IdDTO request) throws Exception {
        // 获取列表
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("standard_equipment_id", request.getId());
        Page<EquipmentStandardVerifyRegulation> verifyRegulationPage = meterStandardVerifyRegulationMapper.selectPage(page, wrapper);
        if (!CollectionUtils.isEmpty(verifyRegulationPage.getRecords())) {
            for (EquipmentStandardVerifyRegulation record : verifyRegulationPage.getRecords()) {
                MeterFile meterFile = meterFileMapper.selectById(record.getVerifyRegulationId().toString());
                if(null != meterFile){
                    BeanUtils.copyProperties(meterFile, record);
                    DictCodeUtils.convertDictCodeToName(record);
                }
            }
            // 将已经不存在的文件排除
            verifyRegulationPage.setRecords(verifyRegulationPage.getRecords().stream().filter(item -> StringUtils.isNotEmpty(item.getFileNo())).collect(Collectors.toList()));
            verifyRegulationPage.setTotal(verifyRegulationPage.getRecords().size());
        }
        return verifyRegulationPage;
    }

    @Override
    public ReturnDTO add(EquipmentStandardVerifyRegulation equipmentStandardVerifyRegulation) throws Exception {
        // 校验
        valid(equipmentStandardVerifyRegulation);
        // 首先判断是否存在，不存在再插入
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("standard_equipment_id", equipmentStandardVerifyRegulation.getStandardEquipmentId());
        wrapper.eq("verify_regulation_id", equipmentStandardVerifyRegulation.getVerifyRegulationId());
        EquipmentStandardVerifyRegulation existRegu = meterStandardVerifyRegulationMapper.selectOne(wrapper);
        if (existRegu == null) {
            meterStandardVerifyRegulationMapper.insert(equipmentStandardVerifyRegulation);
        }
        return ReturnUtil.success();
    }

    @Override
    public List<EquipmentStandardVerifyRegulation> list(Long id) throws Exception {
        // 获取列表
        QueryWrapper<EquipmentStandardVerifyRegulation> wrapper = new QueryWrapper<>();
        wrapper.eq("standard_equipment_id", id);
        List<EquipmentStandardVerifyRegulation> verifyRegulationList = meterStandardVerifyRegulationMapper.selectList(wrapper);
        if (!CollectionUtils.isEmpty(verifyRegulationList)) {
            for (EquipmentStandardVerifyRegulation record : verifyRegulationList) {
                MeterFile meterFile = meterFileMapper.selectById(record.getVerifyRegulationId().toString());
                if(null != meterFile){
                    meterFile.setId(record.getId());
                    BeanUtils.copyProperties(meterFile, record);
                }
            }
        }
        return verifyRegulationList.stream().filter(item -> StringUtils.isNotEmpty(item.getFileNo())).collect(Collectors.toList());
    }

    @Override
    public ReturnDTO batchDelete(List<Long> ids) {
        super.removeBatchByIds(ids);
        return ReturnUtil.success();
    }


    private boolean valid(EquipmentStandardVerifyRegulation equipmentStandardVerifyRegulation) throws Exception {
        // 判断标准装置是否存在
        EquipmentStandardEquipment meterStandardEquipment = meterStandardEquipmentService.getById(equipmentStandardVerifyRegulation.getStandardEquipmentId());
        if (meterStandardEquipment == null) {
            throw new BusinessException(BusinessExceptionEnum.STANDARD_EQUIPMENT_ID_EXIST);
        }
        // 判断文件是否存在
        MeterFile meterFile = meterFileMapper.selectById(equipmentStandardVerifyRegulation.getVerifyRegulationId().toString());
        if (meterFile == null) {
            throw new BusinessException(BusinessExceptionEnum.STANDARD_VERIFY_REGULATION_ID_EXIST);
        }
        return true;
    }

}
