/*
 * Copyright [2022] [https://www.xiaonuo.vip]
 *
 * Snowy采用APACHE LICENSE 2.0开源协议，您在使用过程中，需要注意以下几点：
 *
 * 1.请不要删除和修改根目录下的LICENSE文件。
 * 2.请不要删除和修改Snowy源码头部的版权声明。
 * 3.本项目代码可免费商业使用，商业使用请保留源码和相关描述文件的项目出处，作者声明等。
 * 4.分发源码时候，请注明软件出处 https://www.xiaonuo.vip
 * 5.不可二次分发开源参与同类竞品，如有想法可联系团队xiaonuobase@qq.com商议合作。
 * 6.若您的项目无法满足以上几点，需要更多功能代码，获取Snowy商业授权许可，请在官网购买授权，地址为 https://www.xiaonuo.vip
 */
package vip.xiaonuo.modular.feed.feedingfat.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollStreamUtil;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import vip.xiaonuo.common.enums.CommonSortOrderEnum;
import vip.xiaonuo.common.exception.CommonException;
import vip.xiaonuo.common.page.CommonPageRequest;
import vip.xiaonuo.modular.base.pigpen.entity.BasePigpen;
import vip.xiaonuo.modular.base.pigpen.mapper.BasePigpenMapper;
import vip.xiaonuo.modular.bi.utils.DataUill;
import vip.xiaonuo.modular.feed.feedingenv.entity.FeedingEnv;
import vip.xiaonuo.modular.feed.feedingenv.mapper.FeedingEnvMapper;
import vip.xiaonuo.modular.feed.feedingfat.entity.FeedingFat;
import vip.xiaonuo.modular.feed.feedingfat.mapper.FeedingFatMapper;
import vip.xiaonuo.modular.feed.feedingfat.param.FeedingFatAddParam;
import vip.xiaonuo.modular.feed.feedingfat.param.FeedingFatEditParam;
import vip.xiaonuo.modular.feed.feedingfat.param.FeedingFatIdParam;
import vip.xiaonuo.modular.feed.feedingfat.param.FeedingFatPageParam;
import vip.xiaonuo.modular.feed.feedingfat.service.FeedingFatService;
import vip.xiaonuo.modular.feed.feedingfat.vo.FeedingFatVo;
import vip.xiaonuo.modular.feed.feedingfat.vo.FeedingFatVo2;
import vip.xiaonuo.modular.feed.feedinglist.entity.FeedingList;
import vip.xiaonuo.modular.feed.feedinglist.vo.FeedingListVo2;


import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 精准饲喂母猪采食Service接口实现类
 *
 * @author wang
 * @date  2023/12/28 09:22
 **/
@Service
public class FeedingFatServiceImpl extends ServiceImpl<FeedingFatMapper, FeedingFat> implements FeedingFatService {
    @Autowired
    private FeedingFatMapper fatMapper;
    @Autowired
    private FeedingEnvMapper envMapper;
    @Autowired
    private BasePigpenMapper pigpenMapper;


    @Override
    public FeedingFatVo listFat(FeedingFatAddParam feedingFatAddParam) {
        String orgId = feedingFatAddParam.getOrgId();
        String unitId = feedingFatAddParam.getUnitId();
        QueryWrapper<BasePigpen> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("org_id", orgId).eq("id", unitId);
        List<BasePigpen> basePigpens = pigpenMapper.selectList(queryWrapper);
        List<String> idList = new ArrayList<>();
        if (basePigpens.size() != 0) {
            basePigpens.forEach(item->{
                idList.add(item.getId());
            });
        }
        QueryWrapper<FeedingEnv> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.eq("org_id", orgId);
        if (idList.size() != 0) {
            queryWrapper1.in("unit_id", idList);
        }
        FeedingEnv feedingEnv = envMapper.selectOne(queryWrapper1);//饲养头数
//        List<String> unitList = new ArrayList<>();
//        envs.forEach(i->{
//            unitList.add(i.getDeviceCode());
//        });
        FeedingFatVo feed = fatMapper.getFeed(feedingEnv.getDeviceCode(), orgId);
        double aDouble = Double.parseDouble(feed.getTodayFeed());
        double aDouble1 = Double.parseDouble(feed.getDayWeight());
        double rank = aDouble / aDouble1;
        DecimalFormat def = new DecimalFormat("0.00");
        feed.setFatCount(1);
        feed.setRank(def.format(rank));
        return feed;
    }

    @Override
    public IPage<FeedingFatVo> fatPage(FeedingFatPageParam feedingFatPageParam) {
        String orgId =feedingFatPageParam.getOrgId();
        Integer pageNum = feedingFatPageParam.getCurrent();
        Integer pageSize = feedingFatPageParam.getSize();
        String eartagNum = feedingFatPageParam.getEartagNum();
        QueryWrapper<FeedingFat> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("org_id", orgId).eq(StringUtils.isNotBlank(eartagNum), "eartag_num", eartagNum);
        Page<FeedingFat> page = new Page<>(pageNum,pageSize);
        IPage<FeedingFatVo> fatVoIPage = fatMapper.listFeedPage(page, queryWrapper);
        return fatVoIPage;
    }

    @Override
    public List<FeedingFatVo2> listDetail(FeedingFatEditParam feedingFatEditParam) {
        String orgId = feedingFatEditParam.getOrgId();
        String eartagNum =feedingFatEditParam.getEartagNum();
        String dataType = feedingFatEditParam.getDataType();//数据类型 1为采食 2为体温 3为活跃量 4为膘体
        String timeType = feedingFatEditParam.getTimeType();//时间类型 1为今日 2为本周 3为本月 4为自定义
        if ("".equals(timeType) || null == timeType) {
            timeType = "1";
        }
        List<FeedingFatVo2> list = new ArrayList<>();
        QueryWrapper<FeedingFat> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("org_id", orgId).eq("eartag_num", eartagNum);
        Date timesmorning = DataUill.getTimesmorning();
        DateTime dateTime = DateUtil.beginOfWeek(new Date());
        Date monthmorning = DataUill.getTimesMonthmorning();
        if ("1".equals(dataType)) {
            if ("1".equals(timeType)) {
                queryWrapper.ge("create_time",timesmorning).orderByAsc("create_time");
                List<FeedingFat> fats = fatMapper.selectList(queryWrapper);
                for (FeedingFat fat : fats) {
                    FeedingFatVo2 fatVo2 = new FeedingFatVo2();
                    fatVo2.setValue(fat.getFeedValue());
                    fatVo2.setCreateDate(fat.getCreateTime());
                    list.add(fatVo2);
                }
            }else if ("2".equals(timeType)){
                queryWrapper.ge("create_time", dateTime);
                list = fatMapper.listFeedDetail(queryWrapper);
            }
            else if ("3".equals(timeType)){
                queryWrapper.ge("create_time", monthmorning);
                list = fatMapper.listFeedDetail(queryWrapper);
            }
            else if ("4".equals(timeType)){
                String startTime = feedingFatEditParam.getStartTime() + " 00:00:00";
                String endTime = feedingFatEditParam.getEndTime() + " 23:59:59";
                queryWrapper.between("create_time", startTime,endTime);
                list = fatMapper.listFeedDetail(queryWrapper);
            }
        }
        if ("2".equals(dataType)) {
            if ("1".equals(timeType)) {
                queryWrapper.ge("create_time",timesmorning).orderByAsc("create_time");
                List<FeedingFat> fats = fatMapper.selectList(queryWrapper);
                for (FeedingFat fat : fats) {
                    FeedingFatVo2 fatVo2 = new FeedingFatVo2();
                    fatVo2.setValue(fat.getTemp());
                    fatVo2.setCreateDate(fat.getCreateTime());
                    list.add(fatVo2);
                }
            }else if ("2".equals(timeType)){
                queryWrapper.ge("create_time", dateTime);
                list = fatMapper.listTempDetail(queryWrapper);
            }
            else if ("3".equals(timeType)){
                queryWrapper.ge("create_time", monthmorning);
                list = fatMapper.listTempDetail(queryWrapper);
            }
            else if ("4".equals(timeType)){
                String startTime = feedingFatEditParam.getStartTime() + " 00:00:00";
                String endTime = feedingFatEditParam.getEndTime() + " 23:59:59";
                queryWrapper.between("create_time", startTime,endTime);
                list = fatMapper.listTempDetail(queryWrapper);
            }
        }
        if ("3".equals(dataType)) {
            if ("1".equals(timeType)) {
                queryWrapper.ge("create_time",timesmorning).orderByAsc("create_time");
                List<FeedingFat> fats = fatMapper.selectList(queryWrapper);
                for (FeedingFat fat : fats) {
                    FeedingFatVo2 fatVo2 = new FeedingFatVo2();
                    fatVo2.setValue(fat.getFeedAct());
                    fatVo2.setCreateDate(fat.getCreateTime());
                    list.add(fatVo2);
                }
            }else if ("2".equals(timeType)){
                queryWrapper.ge("create_time", dateTime);
                list = fatMapper.listActDetail(queryWrapper);
            }
            else if ("3".equals(timeType)){
                queryWrapper.ge("create_time", monthmorning);
                list = fatMapper.listActDetail(queryWrapper);
            }
            else if ("4".equals(timeType)){
                String startTime = feedingFatEditParam.getStartTime() + " 00:00:00";
                String endTime = feedingFatEditParam.getEndTime() + " 23:59:59";
                queryWrapper.between("create_time", startTime,endTime);
                list = fatMapper.listActDetail(queryWrapper);
            }
        }
        if ("4".equals(dataType)) {
            if ("1".equals(timeType)) {
                queryWrapper.ge("create_time",timesmorning).orderByAsc("create_time");
                List<FeedingFat> fats = fatMapper.selectList(queryWrapper);
                for (FeedingFat fat : fats) {
                    FeedingFatVo2 fatVo2 = new FeedingFatVo2();
                    fatVo2.setValue(fat.getWeigthValue());
                    fatVo2.setCreateDate(fat.getCreateTime());
                    list.add(fatVo2);
                }
            }else if ("2".equals(timeType)){
                queryWrapper.ge("create_time", dateTime);
                list = fatMapper.listWeightDetail(queryWrapper);
            }
            else if ("3".equals(timeType)){
                queryWrapper.ge("create_time", monthmorning);
                list = fatMapper.listWeightDetail(queryWrapper);
            }
            else if ("4".equals(timeType)){
                String startTime = feedingFatEditParam.getStartTime() + " 00:00:00";
                String endTime = feedingFatEditParam.getEndTime() + " 23:59:59";
                queryWrapper.between("create_time", startTime,endTime);
                list = fatMapper.listWeightDetail(queryWrapper);
            }
        }
        return list;
    }


}
