package com.huimv.admin.controller;


import com.alibaba.fastjson.JSONArray;
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.ProdSaleVo;
import com.huimv.admin.entity.vo.ProdSaleVo2;
import com.huimv.admin.mapper.ProdSaleMapper;
import com.huimv.admin.service.IProdSaleService;
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.util.List;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author author
 * @since 2023-02-22
 */
@RestController
@RequestMapping("/prod-sale")
@CrossOrigin
public class ProdSaleController {

    @Autowired
    private IProdSaleService prodSaleService;

    @Autowired
    private ProdSaleMapper prodSaleMapper;

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

    @RequestMapping("/add")
    public Result add(HttpServletRequest httpServletRequest, @RequestBody ProdSaleVo2 prodSaleVo) {
        String farmId = prodSaleVo.getFarmId();
        List<ProdSale> prodSales = prodSaleVo.getList();
        for (ProdSale prodSale : prodSales) {
            prodSale.setFarmId(Integer.valueOf(farmId));
        }
        prodSaleService.remove(new QueryWrapper<ProdSale>().eq("display_type",1).eq("farm_id",farmId));
        prodSaleService.saveBatch(prodSales);
        return Result.SUCCESS();
    }

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