package com.huimv.admin.controller;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.huimv.admin.common.utils.Result;
import com.huimv.admin.entity.ProdSale;
import com.huimv.admin.entity.ProdStock;
import com.huimv.admin.entity.vo.ProdStockVo;
import com.huimv.admin.entity.vo.ProdStockVo2;
import com.huimv.admin.mapper.ProdStockMapper;
import com.huimv.admin.service.IProdStockService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 存栏结构 前端控制器
 * </p>
 *
 * @author author
 * @since 2023-02-22
 */
@RestController
@RequestMapping("/prod-stock")
@CrossOrigin
public class ProdStockController {

    @Autowired
    private IProdStockService prodStockService;

    @Autowired
    private ProdStockMapper prodStockMapper;

    @RequestMapping("/list")
    public Result list(HttpServletRequest httpServletRequest, @RequestBody Map<String, String> paramsMap) {
        return prodStockService.list(httpServletRequest,paramsMap);
    }

    @RequestMapping("/listStockScreen")
    public Result listStockScreen(HttpServletRequest httpServletRequest, @RequestBody Map<String, String> paramsMap) {
        return prodStockService.listStockScreen(httpServletRequest,paramsMap);
    }

    @RequestMapping("/listStockScreenHenglu")
    public Result listStockScreenHenglu(HttpServletRequest httpServletRequest, @RequestBody Map<String, String> paramsMap) {
        return prodStockService.listStockScreenHenglu(httpServletRequest,paramsMap);
    }
    @RequestMapping("/listStockScreenHengluSale")
    public Result listStockScreenHengluSale(HttpServletRequest httpServletRequest, @RequestBody Map<String, String> paramsMap) {
        return prodStockService.listStockScreenHengluSale(httpServletRequest,paramsMap);
    }
    @RequestMapping("/listStockScreenHengluDay")
    public Result listStockScreenHengluDay(HttpServletRequest httpServletRequest, @RequestBody Map<String, String> paramsMap) {
        return prodStockService.listStockScreenHengluDay(httpServletRequest,paramsMap);
    }

    @RequestMapping("/listStockScreenHengluMonth")
    public Result listStockScreenHengluMonth(HttpServletRequest httpServletRequest, @RequestBody Map<String, String> paramsMap) {
        return prodStockService.listStockScreenHengluMonth(httpServletRequest,paramsMap);
    }
    @RequestMapping("/listStockScreenHengluYear")
    public Result listStockScreenHengluYear(HttpServletRequest httpServletRequest, @RequestBody Map<String, String> paramsMap) {
        return prodStockService.listStockScreenHengluYear(httpServletRequest,paramsMap);
    }

    @RequestMapping("/listWarning")
    public Result listWarning(HttpServletRequest httpServletRequest, @RequestBody Map<String, String> paramsMap) {
        return prodStockService.listWarning(httpServletRequest,paramsMap);
    }


    @RequestMapping("/add")
    public Result add(HttpServletRequest httpServletRequest, @RequestBody ProdStockVo2 prodStockVo2) throws ParseException {
        String farmId = prodStockVo2.getFarmId();
        List<ProdStock> prodStocks = prodStockVo2.getList();
        for (ProdStock prodStock : prodStocks) {
            prodStock.setZcl(prodStock.getMzcl()
                    +prodStock.getGzcl()
                    +prodStock.getBrzcl()
                    +prodStock.getByzcl()
                    +prodStock.getYfzcl()
                    +prodStock.getHbzcl()
            );
            prodStock.setFarmId(Integer.valueOf(farmId));
        }
        prodStockService.remove(new QueryWrapper<ProdStock>().eq("display_type",1).eq("farm_id",farmId));
        prodStockService.saveBatch(prodStocks);
        return Result.SUCCESS();
    }

    @RequestMapping("/update")
    public Result update(@RequestBody ProdStock prodStock) {
        prodStockMapper.updateById(prodStock);
        return Result.SUCCESS();
    }

    @RequestMapping("/delete")
    public Result delete(@RequestBody ProdStock prodStock) {
        prodStockMapper.deleteById(prodStock);
        return Result.SUCCESS();
    }
}
