/*
 * 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.feedingthreshold.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import vip.xiaonuo.modular.feed.feedinglist.entity.FeedingList;
import vip.xiaonuo.modular.feed.feedinglist.mapper.FeedingListMapper;
import vip.xiaonuo.modular.feed.feedinglist.vo.FeedingListVo;
import vip.xiaonuo.modular.feed.feedingthreshold.entity.FeedingThreshold;
import vip.xiaonuo.modular.feed.feedingthreshold.mapper.FeedingThresholdMapper;
import vip.xiaonuo.modular.feed.feedingthreshold.param.AddFeedingParam;
import vip.xiaonuo.modular.feed.feedingthreshold.param.FeedingEditParam;
import vip.xiaonuo.modular.feed.feedingthreshold.param.FeedingThresholdAddParam;
import vip.xiaonuo.modular.feed.feedingthreshold.service.FeedingThresholdService;
import vip.xiaonuo.modular.feed.feedingthreshold.vo.FeedingThresholdVo;
import vip.xiaonuo.modular.feed.feedingthreshold.vo.FeedingThresholdVo2;

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

/**
 * 精准饲喂阈值Service接口实现类
 *
 * @author wang
 * @date  2024/01/02 10:37
 **/
@Service
public class FeedingThresholdServiceImpl extends ServiceImpl<FeedingThresholdMapper, FeedingThreshold> implements FeedingThresholdService {
    @Autowired
    private FeedingThresholdMapper thresholdMapper;
    @Autowired
    private FeedingListMapper listMapper;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void addAndEdit(FeedingThresholdAddParam feedingThresholdAddParam) {
        FeedingThreshold feedingThreshold = BeanUtil.toBean(feedingThresholdAddParam, FeedingThreshold.class);
        FeedingThreshold feedingThreshold1=thresholdMapper.selectOne(new QueryWrapper<FeedingThreshold>().eq("org_id",
                feedingThreshold.getOrgId()).eq("threshold_type",1));
        if (ObjectUtil.isEmpty(feedingThreshold1)) {
            feedingThreshold.setCreateDate(new Date());
            thresholdMapper.insert(feedingThreshold);
        } else {
            BeanUtil.copyProperties(feedingThreshold, feedingThreshold1);
            thresholdMapper.updateById(feedingThreshold1);
        }
    }

    @Override
    public FeedingThresholdVo listFat(String orgId) {
        QueryWrapper<FeedingThreshold> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("org_id", orgId).eq("threshold_type",1);
        FeedingThreshold threshold = thresholdMapper.selectOne(queryWrapper);
        FeedingThresholdVo feedingThresholdVo = new FeedingThresholdVo();
        BeanUtil.copyProperties(threshold, feedingThresholdVo);
        return feedingThresholdVo;
    }

    @Override
    public void addFeed(AddFeedingParam feedingParam) {
        QueryWrapper<FeedingList> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("org_id", feedingParam.getOrgId());
        List<FeedingListVo> feedingListVos = listMapper.listFeed(queryWrapper);
        for (FeedingListVo feedingListVo : feedingListVos) {
            QueryWrapper<FeedingThreshold> queryWrapper1 = new QueryWrapper<>();
            queryWrapper1.eq("org_id", feedingParam.getOrgId()).eq("threshold_type", 2)
                    .eq("feeding_unit_id", feedingListVo.getUnitId());
            if (ObjectUtil.isEmpty(thresholdMapper.selectOne(queryWrapper1))) {
                FeedingThreshold threshold = new FeedingThreshold();
                threshold.setThresholdType(2);
                threshold.setFeedingUnitId(feedingListVo.getUnitId());
                threshold.setFeedingName(feedingListVo.getUnitName());
                threshold.setMarginValue("0");
                thresholdMapper.insert(threshold);
            }
        }
    }

    @Override
    public List<FeedingThresholdVo2> listFeeding(AddFeedingParam feedingParam) {
        QueryWrapper<FeedingThreshold> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("org_id", feedingParam.getOrgId()).eq("threshold_type", 2);
        List<FeedingThreshold> thresholds = thresholdMapper.selectList(queryWrapper);
        List<FeedingThresholdVo2> list = new ArrayList<>();
        for (FeedingThreshold threshold : thresholds) {
            FeedingThresholdVo2 thresholdVo2 = new FeedingThresholdVo2();
            thresholdVo2.setFeedingName(threshold.getFeedingName());
            thresholdVo2.setFeedingUnitId(threshold.getFeedingUnitId());
            thresholdVo2.setMarginValue(threshold.getMarginValue());
            thresholdVo2.setId(threshold.getId().toString());
            thresholdVo2.setOrgId(threshold.getOrgId());
            list.add(thresholdVo2);
        }
        return list;
    }

    @Override
    public void editFeeding(FeedingThresholdVo2 feedingEditParam) {
        FeedingThreshold feedingThreshold = new FeedingThreshold();
        feedingThreshold.setFeedingName(feedingEditParam.getFeedingName());
        feedingThreshold.setFeedingUnitId(feedingEditParam.getFeedingUnitId());
        feedingThreshold.setMarginValue(feedingEditParam.getMarginValue());
        feedingThreshold.setOrgId(feedingEditParam.getOrgId());
        feedingThreshold.setDeleteFlag("NOT_DELETE");
        feedingThreshold.setId(Integer.valueOf(feedingEditParam.getId()));
        thresholdMapper.updateById(feedingThreshold);
    }

}
