package com.huimv.produce.produce.controller;

import com.huimv.common.utils.Result;
import com.huimv.produce.produce.service.IWeight;
import com.huimv.produce.produce.utils.DateUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.util.Date;

/**
 * @Project : huimv.shiwan
 * @Package : com.huimv.biosafety.uface.controller
 * @Description : TODO
 * @Version : 1.0
 * @Author : ZhuoNing
 * @Create : 2020-12-25
 **/
@RestController
@RequestMapping(value = "/weight",method = RequestMethod.GET)
@Slf4j
public class WeightController {
    @Autowired
    private IWeight weight;
    @Autowired
    private DateUtil dateUtil;

    @RequestMapping(value = "/getEggWeightDetails")
    public Result getEggWeightDetails(@RequestParam(value = "date",required = true) String date) throws ParseException {
        //
        return weight.getEggWeightDetails(date);
    }

    // 读取前N天的记录
    @GetMapping("/getManyDayWeight")
    public Result getManyDayWeight(@RequestParam(value = "days",required = true) Integer days,
                                   @RequestParam(value = "poundType",required = true) String poundType) throws ParseException {
        //
        return weight.getManyDayWeight(days,poundType);
    }

    //读取蛋场称重明细明细
    @GetMapping("/getWeightDetails")
    public Result getWeightDetails(@RequestParam(value = "poundType",required = false) String poundType,
                                   @RequestParam(value = "carNumber",required = false) String carNumber,
                                   @RequestParam(value = "consigner",required = false) String consigner,
                                   @RequestParam(value = "consignee",required = false) String consignee,
                                   @RequestParam(value = "variety",required = false) String variety,
                                   @RequestParam(value = "startDate",required = false) String startDate,
                                   @RequestParam(value = "endDate",required = false) String endDate,
                                   @RequestParam(value = "pageNo",required = true) Integer pageNo,
                                   @RequestParam(value = "pageSize",required = true) Integer pageSize) throws ParseException {
        DateUtil du = new DateUtil();
        Date startDate2 = null;
        if(StringUtils.isNotBlank(startDate)){
            startDate2 = du.parseDate(startDate);
        }
        Date endDate2 = null;
        if(StringUtils.isNotBlank(endDate)){
            endDate2 = du.parseDate(endDate);
        }
        //
        return weight.getWeightDetails(poundType, carNumber,consigner,consignee,variety,startDate2 ,endDate2,pageNo,pageSize);
    }

    //指定查询日期期间
    @RequestMapping(value = "/getWeight")
    public Result getDayWeight(@RequestParam(value = "farmId", required = true) Integer farmId, @RequestParam(value = "startDate", required = true) String startDate, @RequestParam(value = "endDate", required = true) String endDate) {
        log.info("farmId>>" + farmId);
        log.info("startDate>>" + startDate);
        log.info("endDate>>" + endDate);
        //
        return weight.getDayWeight(farmId, startDate, endDate);
    }

    //近7次卖猪数据
    @RequestMapping(value = "/getWeightLastNtimes")
    public Result getDayWeightLastNtimes(@RequestParam(value = "farmId", required = true) Integer farmId, @RequestParam(value = "days", required = true) Integer days) {
        log.info("farmId>>" + farmId);
        log.info("days>>" + days);
        //
        return weight.getDayWeightLastNtimes(farmId, days);
    }

    //本月统计
    @RequestMapping(value = "/getWeightInOneMonth")
    public Result getDayWeightInOneMonth(@RequestParam(value = "farmId", required = true) Integer farmId) {
        log.info("farmId>>" + farmId);
        String startDateText = dateUtil.getMonthStart();
        log.info("startDateText>>" + startDateText);
        String endDateText = dateUtil.getMonthEnd();
        log.info("endDateText>>" + endDateText);
        //
        return weight.getDayWeight(farmId, startDateText, endDateText);
    }

    //当天统计
    @RequestMapping(value = "/getWeightOnToday")
    public Result getDayWeightOnToday(@RequestParam(value = "farmId", required = true) Integer farmId) throws ParseException {
        log.info("farmId>>" + farmId);
        String todayDateText = dateUtil.getTodayDateText();
        log.info("startDate>>" + todayDateText);
        log.info("endDate>>" + todayDateText);
        //
        return weight.getDayWeight(farmId, todayDateText, todayDateText);
    }

    //查询所有记录带分页
    @RequestMapping(value = "/getWeightOnPage")
    public Result getWeightOnPage(@RequestParam(value = "farmId", required = true) Integer farmId, @RequestParam(value = "pageSize", required = true) Integer pageSize,
                                  @RequestParam(value = "pageNo", required = true) Integer pageNo) throws ParseException {
        log.info("farmId>>" + farmId);
        String startDate = dateUtil.getYearStart();
        String endDate = dateUtil.getYearEnd();
        log.info("startDate>>" + startDate);
        log.info("endDate>>" + endDate);
        log.info("pageNo>>" + pageNo);
        log.info("pageSize>>" + pageSize);
        //
        return weight.getWeightOnPage(farmId, dateUtil.parseDate(startDate), dateUtil.parseDate(endDate), pageNo, pageSize);
    }

    //分页查询批次重量记录
    @RequestMapping(value = "/getBatchWeightOnPage")
    public Result getBatchWeightOnPage(@RequestParam(value = "farmId", required = true) Integer farmId, @RequestParam(value = "pageSize", required = true) Integer pageSize,
                                       @RequestParam(value = "pageNo", required = true) Integer pageNo,
                                       @RequestParam(value = "addTime", required = false) String addTime) throws ParseException {
        log.info("farmId>>" + farmId);
        log.info("pageNo>>" + pageNo);
        log.info("pageSize>>" + pageSize);
        //
        return weight.getBatchWeightOnPage(farmId, dateUtil.parseDate(addTime), pageNo, pageSize);
    }

    //分页查询批次重量记录
    @RequestMapping(value = "/getBatchWeightOnPage2")
    public Result getBatchWeightOnPage2(@RequestParam(value = "farmId", required = true) Integer farmId, @RequestParam(value = "pageSize", required = true) Integer pageSize,
                                        @RequestParam(value = "pageNo", required = true) Integer pageNo,
                                        @RequestParam(value = "startDate", required = false) String startDate, @RequestParam(value = "endDate", required = false) String endDate) throws ParseException {
        log.info("farmId>>" + farmId);
        log.info("pageNo>>" + pageNo);
        log.info("pageSize>>" + pageSize);
        if(startDate == null){
            startDate = dateUtil.getYearStart();
        }
        if(endDate == null){
            endDate = dateUtil.getYearEnd();
        }
        log.info("startDate>>" + startDate);
        log.info("endDate>>" + endDate);
        //
        return weight.getBatchWeightOnPage2(farmId, dateUtil.parseDate(startDate),dateUtil.parseDate(endDate), pageNo, pageSize);
//        return weight.getBatchWeightOnPage4(farmId, startDate,endDate, pageNo, pageSize);
    }

    //分页查询每次称重记录
    @RequestMapping(value = "/getEveryWeightOnPage")
    public Result getEveryWeightOnPage(@RequestParam(value = "farmId", required = true) Integer farmId, @RequestParam(value = "pageSize", required = true) Integer pageSize,
                                        @RequestParam(value = "pageNo", required = true) Integer pageNo,@RequestParam(value = "days", required = false) Integer days,
                                        @RequestParam(value = "startDate", required = false) String startDate, @RequestParam(value = "endDate", required = false) String endDate) throws ParseException {
        log.info("farmId>>" + farmId);
        log.info("pageNo>>" + pageNo);
        log.info("pageSize>>" + pageSize);
        if(days == null || days.toString().length()==0){
            days = 7;
        }
        if(startDate == null){
            startDate = dateUtil.getPastDate(days);
        }
        if(endDate == null){
            endDate = dateUtil.getTodayDateText();
        }
        log.info("startDate>>" + startDate);
        log.info("endDate>>" + endDate);
        //
        return weight.getEveryTimeWeightOnPage(farmId, dateUtil.parseDate(startDate),dateUtil.parseDate(endDate), pageNo, pageSize);
    }
}
