/*
 * 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.feedinglist.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.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
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.bi.utils.DataUill;
import vip.xiaonuo.modular.feed.feedinglist.entity.FeedingList;
import vip.xiaonuo.modular.feed.feedinglist.mapper.FeedingListMapper;
import vip.xiaonuo.modular.feed.feedinglist.param.FeedingListAddParam;
import vip.xiaonuo.modular.feed.feedinglist.param.FeedingListEditParam;
import vip.xiaonuo.modular.feed.feedinglist.param.FeedingListIdParam;
import vip.xiaonuo.modular.feed.feedinglist.param.FeedingListPageParam;
import vip.xiaonuo.modular.feed.feedinglist.service.FeedingListService;
import vip.xiaonuo.modular.feed.feedinglist.vo.FeedingListVo;
import vip.xiaonuo.modular.feed.feedinglist.vo.FeedingListVo2;


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

/**
 * 料塔Service接口实现类
 *
 * @author wang
 * @date  2023/12/27 15:34
 **/
@Service
public class FeedingListServiceImpl extends ServiceImpl<FeedingListMapper, FeedingList> implements FeedingListService {

    @Autowired
    private FeedingListMapper feedingListMapper;
    @Override
    public Page<FeedingList> page(FeedingListPageParam feedingListPageParam) {
        QueryWrapper<FeedingList> queryWrapper = new QueryWrapper<>();
        if(ObjectUtil.isNotEmpty(feedingListPageParam.getUnitId())) {
            queryWrapper.lambda().notLike(FeedingList::getUnitId, feedingListPageParam.getUnitId());
        }
        if(ObjectUtil.isAllNotEmpty(feedingListPageParam.getSortField(), feedingListPageParam.getSortOrder())) {
            CommonSortOrderEnum.validate(feedingListPageParam.getSortOrder());
            queryWrapper.orderBy(true, feedingListPageParam.getSortOrder().equals(CommonSortOrderEnum.ASC.getValue()),
                    StrUtil.toUnderlineCase(feedingListPageParam.getSortField()));
        } else {
            queryWrapper.lambda().orderByAsc(FeedingList::getId);
        }
        return this.page(CommonPageRequest.defaultPage(), queryWrapper);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void add(FeedingListAddParam feedingListAddParam) {
        FeedingList feedingList = BeanUtil.toBean(feedingListAddParam, FeedingList.class);
        this.save(feedingList);
    }


    @Override
    public List<FeedingListVo> listFeed(String orgId) {
        QueryWrapper<FeedingList> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("org_id", orgId);
        List<FeedingListVo> feedingListVos = feedingListMapper.listFeed(queryWrapper);
        return feedingListVos;
    }

    @Override
    public List<FeedingListVo2> feedingList(Map<String,String> paramsMap) {
        String type = paramsMap.get("type");
        if ("".equals(type) || null == type) {
            type = "1";
        }
        List<FeedingListVo2> list = new ArrayList<>();
        QueryWrapper<FeedingList> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("unit_id", paramsMap.get("unitId"));
        if ("1".equals(type)) {
            Date timesmorning = DataUill.getTimesmorning();
            queryWrapper.ge("create_time",timesmorning).orderByAsc("create_time");
            List<FeedingList> feedingLists = feedingListMapper.selectList(queryWrapper);
            for (FeedingList feedingList : feedingLists) {
                FeedingListVo2 listVo2 = new FeedingListVo2();
                listVo2.setFeedValue(feedingList.getFeedValue());
                listVo2.setMarginValue(feedingList.getMarginValue());
                listVo2.setUploadTime(feedingList.getCreateTime());
                listVo2.setUnitId(feedingList.getUnitId());
                listVo2.setUnitName(feedingList.getUnitName());
                list.add(listVo2);
            }
        } else if ("2".equals(type)) {
            DateTime dateTime = DateUtil.beginOfWeek(new Date());
            queryWrapper.ge("create_time", dateTime);
             list = feedingListMapper.feedList(queryWrapper);
        }else if ("3".equals(type)) {
            Date monthmorning = DataUill.getTimesMonthmorning();
            queryWrapper.ge("create_time", monthmorning);
             list = feedingListMapper.feedList(queryWrapper);
        }else if ("4".equals(type)) {
            String startTime = paramsMap.get("startTime") + " 00:00:00";
            String endTime = paramsMap.get("endTime") + " 23:59:59";
            queryWrapper.between("create_time", startTime,endTime);
             list = feedingListMapper.feedList(queryWrapper);
        }
        return list;
    }
}
