package com.mvc.service.forwarding;

import com.mvc.dao.TForwarderStationMapper;
import com.mvc.entity.TForwarderStation;
import com.mvc.entity.TForwarderStationExample;
import com.mvc.model.StationAccModel;
import com.mvc.model.forwarding.TForwarderStationModel;
import com.mvc.service.taizhang.StationAccService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

@Service
@Transactional
public class TForwarderStationService {
    @Resource
    private TForwarderStationMapper tForwarderStationMapper;

    @Resource
    private StationAccService stationAccService;

    public List<TForwarderStationModel> findAllTForwarderStation(TForwarderStationModel queryModel) {
        TForwarderStationExample example = new TForwarderStationExample();
        TForwarderStationExample.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotEmpty(queryModel.getStationCode())) {
            criteria.andStationCodeLike("%" + queryModel.getStationCode() + "%");
        }
        if (StringUtils.isNotEmpty(queryModel.getCreateMan())) {
            criteria.andCreateManLike("%" + queryModel.getCreateMan() + "%");
        }
        if (queryModel.getForwarderId() != null) {
            criteria.andForwarderIdEqualTo(queryModel.getForwarderId());
        }
        if(StringUtils.isNotEmpty(queryModel.getUseFlag())){
            criteria.andUseFlagEqualTo(queryModel.getUseFlag());
        }

        List<TForwarderStation> forwarderStations = tForwarderStationMapper.selectByExample(example);
        List<TForwarderStationModel> resList = new ArrayList<>();
        for (TForwarderStation forwarderStation : forwarderStations) {
            //为了使不建站点台账也可转发 2024-07-30 guozhao
            /*StationAccModel accModel = stationAccService.findStationAccByStationCode(forwarderStation.getStationCode());
            if (accModel == null) {
                continue;
            }
            if(StringUtils.isNotEmpty(queryModel.getUnitCode())){//客户查询要过滤
                if(!accModel.getUnitCode().equals(queryModel.getUnitCode())&&!accModel.getVisUnitCodes().contains(queryModel.getUnitCode())){
                    continue;
                }
            }*/

            TForwarderStationModel model = new TForwarderStationModel();
            BeanUtils.copyProperties(forwarderStation, model);

            resList.add(model);
        }
        return resList;
    }

    public TForwarderStationModel saveTForwarderStation(TForwarderStationModel model) {
        TForwarderStation TForwarderStation = new TForwarderStation();
        BeanUtils.copyProperties(model, TForwarderStation);
        int insert = tForwarderStationMapper.insert(TForwarderStation);
        model.setId(TForwarderStation.getId());
        return model;
    }

    private String check(TForwarderStationModel model) {
        StationAccModel accModel = stationAccService.findStationAccByStationCode(model.getStationCode());
        if (accModel == null) {
            return "站点编码不在台账中,请先添加台账!";
        }

        model.setStationAccId(model.getId());
        model.setUnitCode(model.getUnitCode());
        return null;
    }

    public TForwarderStationModel updateTForwarderStation(TForwarderStationModel model) {
        TForwarderStation TForwarderStation = tForwarderStationMapper.selectByPrimaryKey(model.getId());
        BeanUtils.copyProperties(model, TForwarderStation);
        int i = tForwarderStationMapper.updateByPrimaryKey(TForwarderStation);
        return model;
    }

    public int deleteTForwarderStation(int id) {
        int i = tForwarderStationMapper.deleteByPrimaryKey(id);
        return i;
    }

    /**
     * 根据遥测站编码查询推送站点列表
     *
     * @param stationCode
     * @return
     */
    public List<TForwarderStationModel> findForwarderStationByStationCode(String stationCode) {
        if (StringUtils.isEmpty(stationCode)) return null;
        List<TForwarderStationModel> reslist = new ArrayList<>();
        TForwarderStationExample example = new TForwarderStationExample();
        TForwarderStationExample.Criteria criteria = example.createCriteria();
        criteria.andStationCodeEqualTo(stationCode);

        List<TForwarderStation> forwarderStations = tForwarderStationMapper.selectByExample(example);
        for (TForwarderStation forwarderStation : forwarderStations) {
            TForwarderStationModel model = new TForwarderStationModel();
            BeanUtils.copyProperties(forwarderStation, model);
            reslist.add(model);
        }
        return reslist;
    }
}
