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


import cn.hutool.core.lang.TypeReference;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.zfire.mall.manager.annotation.ZfireList;
import com.zfire.mall.manager.bean.ExcelData;
import com.zfire.mall.manager.bean.k3.StockPositionStockVO;
import com.zfire.mall.manager.bean.listV2.OrderPlanCustomerVO;
import com.zfire.mall.manager.bean.pick.PickManagerRequest;
import com.zfire.mall.manager.bean.supplier.KingDeeStockVO;
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.plan.PlanLogic;
import com.zfire.mall.manager.plus.entity.OrderPlan;
import com.zfire.mall.manager.plus.entity.OrderPlanCustomer;
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.integration.redis.util.RedisLockRegistry;
import org.springframework.web.bind.annotation.*;

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

/**
 * @author CHANGE
 */
@Slf4j
@RestController
@Api(value = "采购计划单管理", tags ={"采购计划单管理"} )
@RequestMapping(value = "/plan", produces = "application/json; charset=utf-8")
public class PlanController {

    @Autowired
    PlanLogic planLogic;

    @Autowired
    RedisLockRegistry redisLockRegistry;

    @ApiOperation(value = "采购计划单列表")
    @GetMapping("/list")
    public ResponseHelper<IPage<OrderPlan>> list(
            @ApiParam(required = false, value = "单据编号") @RequestParam(required = false) String billNo,
            @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 specification,
            @ApiParam(required = false, value = "产品品类") @RequestParam(required = false) String materialType,
            @ApiParam(value = "页号",required = true)@RequestParam Integer pageNum,
            @ApiParam(value = "页大小",required = true)@RequestParam Integer pageSize
    ) throws Exception {
        IPage<OrderPlan> orderPlan = planLogic.list(billNo,materialName,materialNumber,specification,materialType,pageNum,pageSize);
        return ResponseHelper.success(orderPlan);
    }

    @ApiOperation(value = "采购计划单详情")
    @GetMapping("/detail")
    public ResponseHelper<OrderPlan> detail(
            @ApiParam(required = true, value = "id") @RequestParam(required = true) String id
    ) throws Exception {
        OrderPlan orderPlan = planLogic.detail(id);
        return ResponseHelper.success(orderPlan);
    }


    @ApiOperation(value = "经销商采购计划单列表")
    @GetMapping("/listCustomer")
    public ResponseHelper<IPage<OrderPlanCustomer>> listCustomer(
            @ApiParam(required = false, value = "单据编号") @RequestParam(required = false) String billNo,
            @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 specification,
            @ApiParam(required = false, value = "产品品类") @RequestParam(required = false) String materialType,
            @ApiParam(required = false, value = "经销商名字") @RequestParam(required = false) String customerName,
            @ApiParam(required = false, value = "经销商编码") @RequestParam(required = false) String customerNumber,
            @ApiParam(required = false, value = "销售类型名称") @RequestParam(required = false) String saleName,
            @ApiParam(value = "页号",required = true)@RequestParam Integer pageNum,
            @ApiParam(value = "页大小",required = true)@RequestParam Integer pageSize
    ) throws Exception {
        IPage<OrderPlanCustomer> orderPlanCustomerIPage = planLogic.listCustomer(billNo,materialName,materialNumber,specification,materialType,customerName,customerNumber,saleName,pageNum,pageSize);
        return ResponseHelper.success(orderPlanCustomerIPage);
    }


    @ZfireList
    @PostMapping("/listCustomerV2")
    @ApiOperation("经销商采购计划单-列表V2")
    public ResponseHelper<IPage<OrderPlanCustomerVO>> listCustomerV2(
            @RequestBody ZfireParamBean zfireParamBean
    ) throws RemoteServiceException {
        IPage<OrderPlanCustomerVO> list = planLogic.listCustomerV2(zfireParamBean);
        return ResponseHelper.success(list, new TypeReference<KingDeeStockVO>() {});
    }

    @PostMapping("/listCustomerV2/export")
    @ApiOperation("经销商采购计划单-列表V2导出")
    public void listTollV2Export(
            @RequestBody ZfireParamBean zfireParamBean,
            HttpServletRequest request,
            HttpServletResponse response
    ) throws Exception {
        //1.组装查询条件
        ZfireParamBean zfireParam = FieldUtils.supplyParam(zfireParamBean);
        //2.查询要导出的内容
        IPage<OrderPlanCustomerVO> list = planLogic.listCustomerV2(zfireParamBean);
        //3.导出
        FieldUtils.exportData(list.getRecords(),zfireParam.getExportFields(),request,response);
    }


    @ApiOperation(value = "经销商采购采购计划单详情")
    @GetMapping("/detailCustomer")
    public ResponseHelper<OrderPlanCustomer> detailCustomer(
            @ApiParam(required = true, value = "id") @RequestParam(required = true) String id
    ) throws Exception {
        OrderPlanCustomer orderPlanCustomer = planLogic.detailCustomer(id);
        return ResponseHelper.success(orderPlanCustomer);
    }


    @PostMapping("/submitPlan")
    @ApiOperation("提交经销商采购计划单")
    public ResponseHelper submitPlan(@RequestBody List<String> ids, HttpServletRequest request) throws InterruptedException {
        Lock obtain = redisLockRegistry.obtain(Constant.RedisPrefix.PLAN_ORDER);
        try {
            if (!obtain.tryLock(5, TimeUnit.SECONDS)) {
                throw new RemoteServiceException("系统繁忙，请稍后再试");
            }
            planLogic.submitPlan(ids,request);
        } finally {
            obtain.unlock();
        }
        return ResponseHelper.success();
    }

    @ApiOperation(value = "经销商采购计划单导出")
    @GetMapping("/export")
    public void export(
            @ApiParam(required = false, value = "单据编号") @RequestParam(required = false) String billNo,
            @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 specification,
            @ApiParam(required = false, value = "产品品类") @RequestParam(required = false) String materialType,
            @ApiParam(required = false, value = "经销商名字") @RequestParam(required = false) String customerName,
            @ApiParam(required = false, value = "经销商编码") @RequestParam(required = false) String customerNumber,
            @ApiParam(required = false, value = "销售类型名称") @RequestParam(required = false) String saleName,
            @ApiParam(value = "页号",required = true)@RequestParam Integer pageNum,
            @ApiParam(value = "页大小",required = true)@RequestParam Integer pageSize,
            HttpServletRequest request,
            HttpServletResponse response
    ) throws Exception {
        IPage<OrderPlanCustomer> orderPlanCustomerIPage = planLogic.listCustomer(billNo,materialName,materialNumber,
                specification,materialType,customerName,customerNumber,saleName,1,-1);
        ExcelData excelData = planLogic.export(orderPlanCustomerIPage.getRecords());
        ExcelUtils.exportExcel(request,response,"经销商采购计划.xlsx",excelData);
    }


    @ApiOperation(value = "采购计划单导出")
    @GetMapping("/cExport")
    public void cExport(
            @ApiParam(required = false, value = "单据编号") @RequestParam(required = false) String billNo,
            @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 specification,
            @ApiParam(required = false, value = "产品品类") @RequestParam(required = false) String materialType,
            @ApiParam(value = "页号",required = true)@RequestParam Integer pageNum,
            @ApiParam(value = "页大小",required = true)@RequestParam Integer pageSize,
            HttpServletRequest request,
            HttpServletResponse response
    ) throws Exception {
        IPage<OrderPlan> orderPlan = planLogic.list(billNo,materialName,materialNumber,specification,materialType,pageNum,pageSize);
        ExcelData excelData = planLogic.cExport(orderPlan.getRecords());
        ExcelUtils.exportExcel(request,response,"采购计划单.xlsx",excelData);
    }


}

