/*
Copyright [2020] [https://www.xiaonuo.vip]

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

  http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

Snowy采用APACHE LICENSE 2.0开源协议，您在使用过程中，需要注意以下几点：

1.请不要删除和修改根目录下的LICENSE文件。
2.请不要删除和修改Snowy源码头部的版权声明。
3.请保留源码和相关描述文件的项目出处，作者声明等。
4.分发源码时候，请注明软件出处 https://gitee.com/xiaonuobase/snowy
5.在修改包名，模块名称，项目代码等时，请注明软件出处 https://gitee.com/xiaonuobase/snowy
6.若您的项目无法满足以上几点，可申请商业授权，获取Snowy商业授权许可，请在官网购买授权，地址为 https://www.xiaonuo.vip
 */
package com.huimv.modular.farmstock.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.Query;
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.service.impl.ServiceImpl;
import com.huimv.core.consts.CommonConstant;
import com.huimv.core.enums.CommonStatusEnum;
import com.huimv.core.exception.ServiceException;
import com.huimv.core.factory.PageFactory;
import com.huimv.core.pojo.page.PageResult;
import com.huimv.core.util.PoiUtil;
import com.huimv.modular.farmstock.entity.FarmStock;
import com.huimv.modular.farmstock.enums.FarmStockExceptionEnum;
import com.huimv.modular.farmstock.mapper.FarmStockMapper;
import com.huimv.modular.farmstock.param.FarmStockParam;
import com.huimv.modular.farmstock.service.FarmStockService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 月度统计service接口实现类
 *
 * @author yinhao
 * @date 2021-08-17 14:45:39
 */
@Service
public class FarmStockServiceImpl extends ServiceImpl<FarmStockMapper, FarmStock> implements FarmStockService {

    @Override
    public PageResult<FarmStock> page(FarmStockParam farmStockParam) {
        QueryWrapper<FarmStock> queryWrapper = new QueryWrapper<>();
        if (ObjectUtil.isNotNull(farmStockParam)) {

            // 根据牧场代码 查询
            if (ObjectUtil.isNotEmpty(farmStockParam.getFarmCode())) {
                queryWrapper.lambda().eq(FarmStock::getFarmCode, farmStockParam.getFarmCode());
            }
            // 根据牧场名称 查询
            if (ObjectUtil.isNotEmpty(farmStockParam.getFarmName())) {
                queryWrapper.lambda().eq(FarmStock::getFarmName, farmStockParam.getFarmName());
            }
            // 根据母猪总存栏 查询
            if (ObjectUtil.isNotEmpty(farmStockParam.getSowTotalStock())) {
                queryWrapper.lambda().eq(FarmStock::getSowTotalStock, farmStockParam.getSowTotalStock());
            }
            // 根据母猪新增 查询
            if (ObjectUtil.isNotEmpty(farmStockParam.getEartagNew())) {
                queryWrapper.lambda().eq(FarmStock::getEartagNew, farmStockParam.getEartagNew());
            }
            // 根据耳标离线 查询
            if (ObjectUtil.isNotEmpty(farmStockParam.getEartagOffone())) {
                queryWrapper.lambda().eq(FarmStock::getEartagOffone, farmStockParam.getEartagOffone());
            }
            // 根据能繁母猪存栏 查询
            if (ObjectUtil.isNotEmpty(farmStockParam.getSowStock())) {
                queryWrapper.lambda().eq(FarmStock::getSowStock, farmStockParam.getSowStock());
            }
            // 根据配怀存栏 查询
            if (ObjectUtil.isNotEmpty(farmStockParam.getStock1())) {
                queryWrapper.lambda().eq(FarmStock::getStock1, farmStockParam.getStock1());
            }
            // 根据分娩存栏 查询
            if (ObjectUtil.isNotEmpty(farmStockParam.getStock2())) {
                queryWrapper.lambda().eq(FarmStock::getStock2, farmStockParam.getStock2());
            }
            // 根据空怀存栏 查询
            if (ObjectUtil.isNotEmpty(farmStockParam.getStock3())) {
                queryWrapper.lambda().eq(FarmStock::getStock3, farmStockParam.getStock3());
            }
            // 根据后备存栏 查询
            if (ObjectUtil.isNotEmpty(farmStockParam.getStock4())) {
                queryWrapper.lambda().eq(FarmStock::getStock4, farmStockParam.getStock4());
            }
            // 根据公猪存栏 查询
            if (ObjectUtil.isNotEmpty(farmStockParam.getStock5())) {
                queryWrapper.lambda().eq(FarmStock::getStock5, farmStockParam.getStock5());
            }
            // 根据月份 查询
            if (ObjectUtil.isNotEmpty(farmStockParam.getMonth())) {
                queryWrapper.lambda().eq(FarmStock::getMonth, farmStockParam.getMonth());
            }
            // 根据年度 查询
            if (ObjectUtil.isNotEmpty(farmStockParam.getYear())) {
                queryWrapper.lambda().eq(FarmStock::getYear, farmStockParam.getYear());
            }
            // 根据地市代码 查询
            if (ObjectUtil.isNotEmpty(farmStockParam.getCityCode())) {
                queryWrapper.lambda().eq(FarmStock::getCityCode, farmStockParam.getCityCode());
            }
            // 根据区县代码 查询
            if (ObjectUtil.isNotEmpty(farmStockParam.getCountyCode())) {
                queryWrapper.lambda().eq(FarmStock::getCountyCode, farmStockParam.getCountyCode());
            }
        }
        return new PageResult<>(this.page(PageFactory.defaultPage(), queryWrapper));
    }

    @Override
    public List<FarmStock> list(FarmStockParam farmStockParam) {
        return this.list();
    }

    @Override
    public void add(FarmStockParam farmStockParam) {
        FarmStock farmStock = new FarmStock();
        BeanUtil.copyProperties(farmStockParam, farmStock);
        this.save(farmStock);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void delete(List<FarmStockParam> farmStockParamList) {
        farmStockParamList.forEach(farmStockParam -> {
            this.removeById(farmStockParam.getId());
        });
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void edit(FarmStockParam farmStockParam) {
        FarmStock farmStock = this.queryFarmStock(farmStockParam);
        BeanUtil.copyProperties(farmStockParam, farmStock);
        this.updateById(farmStock);
    }

    @Override
    public FarmStock detail(FarmStockParam farmStockParam) {
        return this.queryFarmStock(farmStockParam);
    }

    /**
     * 获取月度统计
     *
     * @author yinhao
     * @date 2021-08-17 14:45:39
     */
    private FarmStock queryFarmStock(FarmStockParam farmStockParam) {
        FarmStock farmStock = this.getById(farmStockParam.getId());
        if (ObjectUtil.isNull(farmStock)) {
            throw new ServiceException(FarmStockExceptionEnum.NOT_EXIST);
        }
        return farmStock;
    }

    @Override
    public void export(FarmStockParam farmStockParam) {
        List<FarmStock> list = this.list(farmStockParam);
        PoiUtil.exportExcelWithStream("SnowyFarmStock.xls", FarmStock.class, list);
    }




}
