package com.huimv.admin.controller.secondary;


import com.huimv.admin.domain.secondary.DaLqda;
import com.huimv.admin.domain.secondary.DaZsda;
import com.huimv.admin.repo.secondary.DaLqdaRepository;
import com.huimv.admin.repo.secondary.DaZsdaRepository;
import com.huimv.admin.service.secondary.DaLqdaService;
import com.huimv.admin.utils.Result;
import com.huimv.admin.utils.ResultCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

@RestController
@CrossOrigin
@RequestMapping("/DaLqda")
public class DaLqdaController {

    @Autowired
    private DaLqdaService service;

    @Autowired
    private DaZsdaRepository daZsdaRepository ;
    @Autowired
    private DaLqdaRepository DaLqdaRepository ;


    @RequestMapping("/add")
    public Result add(DaLqda childcareEntity, String tJtime) {
        Date date = new Date();
        //体重统计时间
//        childcareEntity.setTztjsj(DateUtils.parseDate(tJtime, DateUtils.DATETIME_FORMAT));
        childcareEntity.setXgsj(date);
        System.out.println("收到数据");

        return service.add(childcareEntity);
    }



    @RequestMapping("/remove")
    public Result remove(Integer[] ids) {
        return service.remove(ids);
    }


    @RequestMapping("/update")
    public Result update(DaLqda childcareEntity) {
        return service.update(childcareEntity);
    }


    @RequestMapping("/find")
    public Result findAllById(String name, Integer pageNum, Integer pageSize) {
        List<DaZsda> zsdaList =daZsdaRepository.findAll(name,pageNum,pageSize );  //findAlla();
        List list02 = new ArrayList();
        for (DaZsda daZsda : zsdaList) {
            Map map = new HashMap();
            List<DaLqda> allByZsid = DaLqdaRepository.findAllByZsid(daZsda.getId());
            if (allByZsid != null && allByZsid.size()>0){
                map.put("data", allByZsid.get(0));
                map.put("zsmc", daZsda.getZsmc());
                list02.add(map);
            }
        }
        return new Result(ResultCode.SUCCESS,list02);
    }



   /* @RequestMapping("/findAll")  //查单个  栏期下面的所有记录数据   点开查看详细信息
    //猪舍id
    public Result findAll(String name, Integer pageNum, Integer pageSize) {
            List<DaZsda> zsdaList = daZsdaRepository.findAll(name, pageNum, pageSize);  //findAlla();
            List list02 = new ArrayList();
            for (DaZsda daZsda : zsdaList) {
                Map map = new HashMap();
                List<DaLqda> allByZsid = DaLqdaRepository.findAllByZsid(daZsda.getId());
                if (allByZsid != null && allByZsid.size() > 0) {
                    map.put("data", allByZsid.get(0));
                    map.put("zsmc", daZsda.getZsmc());

                    list02.add(map);
                }
            }
            int totalsize = list02.size();
            int totalpage = totalsize / pageSize;
            List<Object> list01 = pageBySubList(list02, pageSize, pageNum);
            //总条数
            Map<String, Object> map = new HashMap<>();
            map.put("totalSize", totalsize);
            map.put("totalPage", totalpage);
            List<Object> list = new LinkedList<>();
            list.add(list01);
            list.add(map);
            // 总页数
            return new Result(ResultCode.SUCCESS, list);
        }*/

    @RequestMapping("/findAll")  //查单个  栏期下面的所有记录数据   点开查看详细信息
    //猪舍id
    public Result findAll(String name, Integer pageNum, Integer pageSize) {
        List<DaZsda> listZsda = daZsdaRepository.findAlla();
        List<Integer> zsdaId = new ArrayList();
        for (DaZsda daZsda : listZsda) {
            zsdaId.add(daZsda.getId());
        }
        Integer startPage = (pageNum-1) * pageSize;

        List<Map<String,Object>> allByZsids = DaLqdaRepository.findAllByZsids(zsdaId, name, startPage, pageSize);
        Integer size = DaLqdaRepository.findAllByZsidAll(zsdaId,name);
        Map map = new HashMap();
        map.put("total",size);
        map.put("data",getSelectLow(allByZsids));

        return  new Result(ResultCode.SUCCESS,map);
    }


    public  List<Map<String, Object>> getSelectLow(List<Map<String, Object>> list){
        List<Map<String, Object>> select = new ArrayList<>();

        for (Map<String, Object> stringObjectMap : list) {
            Map map =new HashMap();
            for (String s : stringObjectMap.keySet()) {
                map.put(s.toLowerCase(),stringObjectMap.get(s));
            }
            select.add(map);
        }

        return select;
    }


    //list 分页工具
    public static List<Object> pageBySubList(List list, int pagesize,  int currentPage) {
        int totalcount = list.size();
        int pagecount = 0;
        List<Object> subList;
        int m = totalcount % pagesize;
        if (m > 0) {
            pagecount = totalcount / pagesize + 1;
        } else {
            pagecount = totalcount / pagesize;
        }
        if (m == 0) {
            subList = list.subList((currentPage - 1) * pagesize, pagesize * (currentPage));
        } else {
            if (currentPage == pagecount) {
                subList = list.subList((currentPage - 1) * pagesize, totalcount);
            } else {
                subList = list.subList((currentPage - 1) * pagesize, pagesize * (currentPage));
            }
        }
        return subList;
    }
}





