package com.huimv.produce.produce.service.impl;

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.huimv.common.utils.Result;
import com.huimv.common.utils.ResultCode;
import com.huimv.produce.produce.entity.WeightInformation;
import com.huimv.produce.produce.entity.dto.WeightInformationVo;
import com.huimv.produce.produce.mapper.WeightInformationMapper;
import com.huimv.produce.produce.service.IWeightInformationService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author author
 * @since 2022-03-30
 */
@Service
public class WeightInformationServiceImpl extends ServiceImpl<WeightInformationMapper, WeightInformation> implements IWeightInformationService {


    @Autowired
    private WeightInformationMapper weightInformationMapper;

    @Override
    public Result pageWeight(Map<String,String> map) {
        String pageSize = map.get("pageSize");
        String current = map.get("current");
        String startDate = map.get("startDate");
        String endDate = map.get("endDate");
        String variety = map.get("variety");
        String stationName = map.get("stationName");


        if (StringUtils.isBlank(pageSize)){
            pageSize = "10";
        }
        if (StringUtils.isBlank(current)){
            current = "1";
        }
        QueryWrapper<WeightInformation> wrapper = new QueryWrapper<>();

        wrapper.ge(StringUtils.isNotBlank(startDate),"weighing_time",startDate);
        wrapper.le(StringUtils.isNotBlank(endDate),"weighing_time",endDate);

        wrapper.eq(StringUtils.isNotBlank(variety),"variety",variety);
        wrapper.eq(StringUtils.isNotBlank(stationName),"stationname",stationName);

        Page<WeightInformation> page = this.page(new Page<WeightInformation>(Long.parseLong(current), Long.parseLong(pageSize)), wrapper);


        return new Result(ResultCode.SUCCESS,page);
    }

    @Override
    public Result dayWeight(Map<String, String> map) {
        String startDate = map.get("startDate");
        String endDate = map.get("endDate");
        String variety = map.get("variety");
        String stationName = map.get("stationName");


        QueryWrapper<WeightInformation> wrapper = new QueryWrapper<>();
        wrapper.ge(StringUtils.isNotBlank(startDate),"weighing_time",startDate);
        wrapper.le(StringUtils.isNotBlank(endDate),"weighing_time",endDate);
        wrapper.eq(StringUtils.isNotBlank(variety),"variety",variety);
        wrapper.eq(StringUtils.isNotBlank(stationName),"stationname",stationName);


        List<WeightInformationVo> list = weightInformationMapper.dayWeight(wrapper);
        return new Result(ResultCode.SUCCESS,list);
    }
}
