package com.huimv.video.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.huimv.video.domain.*;
import com.huimv.video.repo.*;
import com.huimv.video.result.Result;
import com.huimv.video.service.AllDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Project : huimv.shiwan
 * @Package : com.huimv.video.service.impl
 * @Description : TODO
 * @Author : yuxuexuan
 * @Create : 2021/3/31 0031 14:07
 **/
@Service
public class AllDataServiceImpl implements AllDataService {
    @Autowired
    private DaZsdaEntityRepository zsdaEntityRepo;
    @Autowired
    private DaLqdaEntityRepository lqdaEntityRepo;

    @Autowired
    private SjHkAqRepository aqRepository;
    @Autowired
    private SjHkSdRepository sdRepository;
    @Autowired
    private SjHkWdRepository wdRepository;
    @Autowired
    private SjHkSlRepository slRepository;
    @Autowired
    private SjHkDlRepository dlRepository;
    @Autowired
    private SjHkPhRepository phRepository;


    @Override
    @DS("slave")
    public Result getZsda() {
        Integer mcid = 39;
        //--读取所有猪舍
        List<DaZsdaEntity> zsdsList = zsdaEntityRepo.getAllZs(mcid);

        List zsidList = new ArrayList();
        for (DaZsdaEntity zsdaEntity : zsdsList) {
            zsidList.add(zsdaEntity.getID());
        }
        //--读取所有栏期
        List<DaLqdaEntity> lqdaEntityList = lqdaEntityRepo.getLqid(zsidList);


        List<Map> zsxx = new ArrayList();
        Map map = new HashMap();
        for (DaZsdaEntity zsdaEntity : zsdsList) {
            int zsid = zsdaEntity.getID();


            map.put("zsid", zsid);
            map.put("zsmc", zsdaEntity.getZSMC());
            List<Integer>  lqidList  = new ArrayList();
            for (DaLqdaEntity lqdaEntity : lqdaEntityList) {
                if (lqdaEntity.getZSID() == zsid) {
                    Integer lqid = lqdaEntity.getID();
                    map.put("lqid", lqid);
                    lqidList.add(lqid);
                    break;
                }
            }


            for (Integer lqid : lqidList) {
                List<SjHkAq> byLqidOrderById = aqRepository.findByLqidOrderById(lqid);
                if ( byLqidOrderById.size()> 0){
                    map.put("aq",byLqidOrderById.get(0).getDqaq()) ;
                }else{
                    map.put("aq",null) ;
                }

                List<SjHkWd> byLqidOrderById1 = wdRepository.findByLqidOrderById(lqid);
                if ( byLqidOrderById1.size()> 0){
                    map.put("wd",byLqidOrderById1.get(0).getDqwd()) ;
                }else{
                    map.put("wd",null) ;
                }

                List<SjHkSd> byLqidOrderById2 = sdRepository.findByLqidOrderById(lqid);
                if ( byLqidOrderById2.size()> 0){
                    map.put("sd",byLqidOrderById2.get(0).getDqsd()) ;
                }else{
                    map.put("sd",null) ;
                }
            }


            zsxx.add(map);
        }


        return new Result(10000,"成功",zsxx);
    }
}
