package com.fastbee.crop.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.fastbee.crop.mapper.AgricultureCropYieldMapper;
import com.fastbee.crop.domain.AgricultureCropYield;
import com.fastbee.crop.service.IAgricultureCropYieldService;

/**
 * 作物产量记录Service业务层处理
 * 
 * @author kerwincui
 * @date 2024-11-26
 */
@Service
public class AgricultureCropYieldServiceImpl implements IAgricultureCropYieldService 
{
    @Autowired
    private AgricultureCropYieldMapper agricultureCropYieldMapper;

    /**
     * 查询作物产量记录
     * 
     * @param id 作物产量记录主键
     * @return 作物产量记录
     */
    @Override
    public AgricultureCropYield selectAgricultureCropYieldById(Long id)
    {
        return agricultureCropYieldMapper.selectAgricultureCropYieldById(id);
    }

    /**
     * 查询作物产量记录列表
     * 
     * @param agricultureCropYield 作物产量记录
     * @return 作物产量记录
     */
    @Override
    public List<AgricultureCropYield> selectAgricultureCropYieldList(AgricultureCropYield agricultureCropYield)
    {
        return agricultureCropYieldMapper.selectAgricultureCropYieldList(agricultureCropYield);
    }

    /**
     * 新增作物产量记录
     * 
     * @param agricultureCropYield 作物产量记录
     * @return 结果
     */
    @Override
    public int insertAgricultureCropYield(AgricultureCropYield agricultureCropYield)
    {
        return agricultureCropYieldMapper.insertAgricultureCropYield(agricultureCropYield);
    }

    /**
     * 修改作物产量记录
     * 
     * @param agricultureCropYield 作物产量记录
     * @return 结果
     */
    @Override
    public int updateAgricultureCropYield(AgricultureCropYield agricultureCropYield)
    {
        return agricultureCropYieldMapper.updateAgricultureCropYield(agricultureCropYield);
    }

    /**
     * 批量删除作物产量记录
     * 
     * @param ids 需要删除的作物产量记录主键
     * @return 结果
     */
    @Override
    public int deleteAgricultureCropYieldByIds(Long[] ids)
    {
        return agricultureCropYieldMapper.deleteAgricultureCropYieldByIds(ids);
    }

    /**
     * 删除作物产量记录信息
     * 
     * @param id 作物产量记录主键
     * @return 结果
     */
    @Override
    public int deleteAgricultureCropYieldById(Long id)
    {
        return agricultureCropYieldMapper.deleteAgricultureCropYieldById(id);
    }

    @Override
    public List<?> selectAgricultureCropYieldStatistics(AgricultureCropYield agricultureCropYield) {
        List<AgricultureCropYield> list = new LambdaQueryChainWrapper<>(agricultureCropYieldMapper)
                .select(AgricultureCropYield::getYieldValue,AgricultureCropYield::getHarvestYear,AgricultureCropYield::getHarvestMonth,AgricultureCropYield::getHarvestMonth,AgricultureCropYield::getId)
                .eq(agricultureCropYield.getCropType()!=null,AgricultureCropYield::getCropType, agricultureCropYield.getCropType())
                .eq(agricultureCropYield.getHarvestYear()!=null,AgricultureCropYield::getHarvestYear,agricultureCropYield.getHarvestYear())
                .list();
        //分组统计不同月份的
        Map<String, List<AgricultureCropYield>> collect = list.stream().collect(Collectors.groupingBy(AgricultureCropYield::getHarvestMonth));
        List<HashMap<String, Object>> result = new ArrayList<>();
        collect.forEach((month, cropYieldList) -> {
            HashMap<String, Object> props = new HashMap<>();
            props.put("name", month+"月");
            props.put("value",cropYieldList.get(0).getYieldValue());
            result.add(props);

        });

        return result;
    }
}
