package com.zfire.mall.manager.controller.stock;


import cn.hutool.core.lang.TypeReference;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.zfire.mall.manager.annotation.ApiNotAuth;
import com.zfire.mall.manager.annotation.ZfireList;
import com.zfire.mall.manager.bean.ExcelData;
import com.zfire.mall.manager.bean.displace.order.DisplaceOrderBean;
import com.zfire.mall.manager.bean.invoice.StockStatusBean;
import com.zfire.mall.manager.bean.k3.StockPositionStock;
import com.zfire.mall.manager.bean.stock.*;
import com.zfire.mall.manager.constant.Constant;
import com.zfire.mall.manager.exception.RemoteServiceException;
import com.zfire.mall.manager.helper.ResponseHelper;
import com.zfire.mall.manager.logic.stock.StockCommonLogic;
import com.zfire.mall.manager.logic.stock.StockCostLogic;
import com.zfire.mall.manager.plus.entity.SysDict;
import com.zfire.mall.manager.utils.excel.ExcelUtils;
import com.zfire.mall.manager.zfire.bean.ZfireParamBean;
import com.zfire.mall.manager.zfire.util.FieldUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock;

/**
 * @author CHANGE
 */
@Slf4j
@RestController
@Api(value = "仓租费用接口", tags ={"仓租费用接口"} )
@RequestMapping(value = "/stock/cost", produces = "application/json; charset=utf-8")
public class StockCostController {

    @Autowired
    StockCostLogic stockCostLogic;



    @ApiOperation(value = "订单满货通知")
    @GetMapping("/list")
    public ResponseHelper<IPage<OrderFullCostVO>> list(
            @ApiParam(required = false, value = "物料名称") @RequestParam(required = false) String materialName,
            @ApiParam(required = false, value = "物料编码") @RequestParam(required = false) String materialNumber,
            @ApiParam(required = false, value = "物料代码") @RequestParam(required = false) String materialOldNumber,
            @ApiParam(required = false, value = "规格型号") @RequestParam(required = false) String specification,
            @ApiParam(required = false, value = "订单号") @RequestParam(required = false) String orderId,
            @ApiParam(required = false, value = "货满时间开始") @RequestParam(required = false) String startTime,
            @ApiParam(required = false, value = "货满时间结束") @RequestParam(required = false) String endTime,
            @ApiParam(value = "页号",required = true)@RequestParam Integer pageNum,
            @ApiParam(value = "页大小",required = true)@RequestParam Integer pageSize
    ) throws Exception {
        IPage<OrderFullCostVO> orderFullCostVOIPage = stockCostLogic.list(orderId,startTime,endTime,materialOldNumber,materialName,materialNumber,specification,pageNum,pageSize);
        return ResponseHelper.success(orderFullCostVOIPage);
    }


    @ZfireList
    @ApiOperation(value = "订单满货生成账单记录")
    @PostMapping("/listFullDate")
    public ResponseHelper<IPage<OrderFullDateVO>> listFullDate(
            @RequestBody ZfireParamBean zfireParamBean

    ) throws Exception {
        IPage<OrderFullDateVO> orderFullCostVOIPage = stockCostLogic.listFullDate(zfireParamBean);
        return ResponseHelper.success(orderFullCostVOIPage, new TypeReference<OrderFullDateVO>() {});
    }




    @ZfireList
    @PostMapping("/listFullCost")
    @ApiOperation("经销商仓储费日账单列表-列表V2")
    public ResponseHelper<IPage<OrderFullCostDayVO>> listFullCost(
            @RequestBody ZfireParamBean zfireParamBean
    ) throws RemoteServiceException {
        IPage<OrderFullCostDayVO> list = stockCostLogic.listFullCost(zfireParamBean);
        return ResponseHelper.success(list, new TypeReference<OrderFullCostDayVO>() {});
    }


    @PostMapping("/listCustomerV2/export")
    @ApiOperation("经销商仓储费日账单列表-列表V2导出")
    public void listFullCostExport(
            @RequestBody ZfireParamBean zfireParamBean,
            HttpServletRequest request,
            HttpServletResponse response
    ) throws Exception {
        //1.组装查询条件
        ZfireParamBean zfireParam = FieldUtils.supplyParam(zfireParamBean);
        //2.查询要导出的内容
        IPage<OrderFullCostDayVO> list = stockCostLogic.listFullCost(zfireParamBean);
        //3.导出
        FieldUtils.exportData(list.getRecords(),zfireParam.getExportFields(),request,response);
    }

    @ApiOperation(value = "剔除")
    @PostMapping("/updateOrderFull")
    public ResponseHelper updateOrderFull(
            @ApiParam(value = "值") @RequestParam(required = true)  List<String> id
    ) throws Exception {

        stockCostLogic.updateOrderFull(id);
        return ResponseHelper.success();

    }



    @GetMapping("/dict")
    @ApiOperation("起始库存")
    public ResponseHelper<SysDict> sysDict(
    ) throws RemoteServiceException {
        SysDict sysDicts = stockCostLogic.sysDict();
        return ResponseHelper.success(sysDicts);
    }


    @ApiOperation(value = "改变起始库存")
    @PostMapping("/update")
    public ResponseHelper tradeAdd(
            @ApiParam(value = "值") @RequestParam(required = true) String dictValue
    ) throws Exception {

        stockCostLogic.update(dictValue);
        return ResponseHelper.success();

    }


   /* @GetMapping("/dictStock")
    @ApiOperation("计算仓库列表")
    public ResponseHelper<List<SysDict>> sysDictList(
            @ApiParam(required = true, value = "GUANGZHOUCOST 广州 FOSHANCOST 佛山") String dictCode

    ) throws RemoteServiceException {
        List<SysDict> sysDicts = stockCostLogic.getSysDictList(dictCode);
        return ResponseHelper.success(sysDicts);
    }


    @ApiOperation(value = "改变起始库存")
    @PostMapping("/updateCost")
    public ResponseHelper updateCost(
            @ApiParam(required = true, value = "dictType传广州仓库GUANGZHOUCOST和佛山仓库FOSHANCOST") @RequestBody List<SysDict> sysDict
    ) throws Exception {

        stockCostLogic.updateCost(sysDict);
        return ResponseHelper.success();

    }*/



    @ZfireList
    @PostMapping("/listMonthFullCost")
    @ApiOperation("仓储费月账单列表-列表V2")
    public ResponseHelper<IPage<OrderFullCostMonthVO>> listMonthFullCost(
            @RequestBody ZfireParamBean zfireParamBean
    ) throws RemoteServiceException {
        IPage<OrderFullCostMonthVO> list = stockCostLogic.listMonthFullCost(zfireParamBean);
        return ResponseHelper.success(list, new TypeReference<OrderFullCostMonthVO>() {});
    }


    @PostMapping("/listMonthFullCost/export")
    @ApiOperation("仓储费月账单列表-列表V2导出")
    public void listMonthFullCostExport(
            @RequestBody ZfireParamBean zfireParamBean,
            HttpServletRequest request,
            HttpServletResponse response
    ) throws Exception {
        //1.组装查询条件
        ZfireParamBean zfireParam = FieldUtils.supplyParam(zfireParamBean);
        //2.查询要导出的内容
        IPage<OrderFullCostMonthVO> list = stockCostLogic.listMonthFullCost(zfireParamBean);
        //3.导出
        FieldUtils.exportData(list.getRecords(),zfireParam.getExportFields(),request,response);
    }


    @ApiNotAuth
    @ApiOperation(value = "生成账单，每次生成一天数据")
    @GetMapping("/syncFullCost")
    public ResponseHelper syncFullCost(
    ) throws Exception {

        stockCostLogic.syncFullCost();
        return ResponseHelper.success();

    }


    @ApiNotAuth
    @ApiOperation(value = "生成未来15天得账单，每次生成一天数据")
    @GetMapping("/syncFullCostFive")
    public ResponseHelper syncFullCostFive(
    ) throws Exception {

        stockCostLogic.syncFullCostFive();
        return ResponseHelper.success();

    }


    @ApiOperation(value = "改变订单是否生成仓储账单")
    @PostMapping("/updateOrderCost")
    public ResponseHelper updateOrderCost(
            @ApiParam(value = "销售订单") @RequestParam(required = true) List<String> orderId,
            @ApiParam(value = "免仓日期") @RequestParam(required = true) Date time
    ) throws Exception {

        stockCostLogic.updateOrderCost(orderId,time);
        return ResponseHelper.success();

    }



    @ApiNotAuth
    @ApiOperation(value = "估算库存")
    @GetMapping("/syncStockNum")
    public ResponseHelper syncStockNum(
    ) throws Exception {

        stockCostLogic.syncStockNum();
        return ResponseHelper.success();

    }




    @ApiNotAuth
    @ApiOperation(value = "生成账单")
    @GetMapping("/syncStockPrice")
    public ResponseHelper syncStockPrice(
    ) throws Exception {

        //stockCostLogic.syncStockPrice();
        return ResponseHelper.success();

    }


    @ApiNotAuth
    @ApiOperation(value = "生成账单engin")
    @GetMapping("/syncStockPriceEngin")
    public ResponseHelper syncStockPriceEngin(
    ) throws Exception {

       // stockCostLogic.syncStockPriceEngin();
        return ResponseHelper.success();

    }


    @ApiNotAuth
    @ApiOperation(value = "生成3.21之后订单")
    @GetMapping("/syncStockThree")
    public ResponseHelper syncStockThree(
    ) throws Exception {

       // stockCostLogic.syncStockThree();
        return ResponseHelper.success();

    }



}

