package com.gree.mall.manager.controller.material.stock;

import cn.hutool.core.lang.TypeReference;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.gree.mall.manager.annotation.ZfireList;
import com.gree.mall.manager.bean.listvo.material.WorkerStockAccPVO;
import com.gree.mall.manager.bean.listvo.material.WorkerStockAccVO;
import com.gree.mall.manager.bean.listvo.material.WorkerStockPVO;
import com.gree.mall.manager.bean.listvo.material.WorkerStockVO;
import com.gree.mall.manager.enums.material.WebsitGoodsTypeEnum;
import com.gree.mall.manager.helper.ResponseHelper;
import com.gree.mall.manager.logic.material.stock.WorkerStockLogic;
import com.gree.mall.manager.utils.excel.ExcelUtils;
import com.gree.mall.manager.zfire.bean.ZfireParamBean;
import com.gree.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.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@Slf4j
@RestController
@Api(value = "库存管理-师傅", tags ={"库存管理-师傅"} )
@Validated
@RequestMapping(value = "/worker/stock", produces = "application/json; charset=utf-8")
public class WorkerStockController {

    @Autowired
    WorkerStockLogic workerStockLogic;
    @Resource
    RedisLockRegistry redisLockRegistry;

    @ZfireList
    @PostMapping("list")
    @ApiOperation(value = "辅材师傅库存-列表")
    public ResponseHelper<IPage<WorkerStockVO>> workerStockList(
            @RequestBody ZfireParamBean zfireParamBean
    ) {
        IPage<WorkerStockVO> page = workerStockLogic.workerStockList(zfireParamBean);
        return ResponseHelper.success(page, new TypeReference<WorkerStockVO>() {});
    }

    @ZfireList
    @PostMapping("listP")
    @ApiOperation(value = "配件师傅库存-列表")
    public ResponseHelper<IPage<WorkerStockPVO>> websitStockPList(
            @RequestBody ZfireParamBean zfireParamBean
    ) {
        IPage<WorkerStockPVO> page = workerStockLogic.workerStockPList(zfireParamBean);
        return ResponseHelper.success(page, new TypeReference<WorkerStockPVO>() {});
    }

    @PostMapping("list/export")
    @ApiOperation("辅材师傅库存-导出")
    public void mListExport(
            @RequestBody ZfireParamBean zfireParamBean,
            HttpServletRequest request,
            HttpServletResponse response
    ) throws Exception {
        //2.查询要导出的内容
        IPage<WorkerStockVO> page = workerStockLogic.workerStockList(zfireParamBean);
        //3.导出
        FieldUtils.exportData(page.getRecords(), zfireParamBean.getExportFields(), request, response);
    }

    @PostMapping("/del")
    @ApiOperation(value = "辅材师傅库存-删除")
    public ResponseHelper delStock(
            @ApiParam(value = "id", required = true) @RequestBody List<String> ids
    ) throws Exception {
        workerStockLogic.delStock(ids);
        return ResponseHelper.success();
    }

    @PostMapping("listP/export")
    @ApiOperation("配件师傅库存-导出")
    public void pListExport(
            @RequestBody ZfireParamBean zfireParamBean,
            HttpServletRequest request,
            HttpServletResponse response
    ) throws Exception {
        //2.查询要导出的内容
        IPage<WorkerStockPVO> page = workerStockLogic.workerStockPList(zfireParamBean);
        //3.导出
        FieldUtils.exportData(page.getRecords(), zfireParamBean.getExportFields(), request, response);
    }



    @PostMapping("importM")
    @ApiOperation("辅材师傅库存-导入(模板名称：辅材师傅库存.xlsx)")
    public ResponseHelper importData(MultipartFile file) throws Exception {
        List<Object> objects = ExcelUtils.importExcel(file);
        workerStockLogic.importData(objects, WebsitGoodsTypeEnum.M);
        return ResponseHelper.success();
    }


    @PostMapping("importP")
    @ApiOperation("配件师傅库存-导入(模板名称：配件师傅库存.xlsx)")
    public ResponseHelper importDataP(MultipartFile file) throws Exception {
        List<Object> objects = ExcelUtils.importExcel(file);
        workerStockLogic.importData(objects, WebsitGoodsTypeEnum.P);
        return ResponseHelper.success();
    }


    @ZfireList
    @PostMapping("acc/list")
    @ApiOperation(value = "辅材库存明细-列表")
    public ResponseHelper<IPage<WorkerStockAccVO>> websitStockAccList(
            @RequestBody ZfireParamBean zfireParamBean
    ) {
        IPage<WorkerStockAccVO> page = workerStockLogic.workerStockAccList(zfireParamBean);
        return ResponseHelper.success(page, new TypeReference<WorkerStockAccVO>() {});
    }

    @PostMapping("acc/list/export")
    @ApiOperation("辅材师傅库存-导出")
    public void accListExport(
            @RequestBody ZfireParamBean zfireParamBean,
            HttpServletRequest request,
            HttpServletResponse response
    ) throws Exception {
        //2.查询要导出的内容
        IPage<WorkerStockAccVO> page = workerStockLogic.workerStockAccList(zfireParamBean);
        //3.导出
        FieldUtils.exportData(page.getRecords(), zfireParamBean.getExportFields(), request, response);
    }



    @ZfireList
    @PostMapping("acc/listP")
    @ApiOperation(value = "配件库存明细-列表")
    public ResponseHelper<IPage<WorkerStockAccPVO>> workerStockAccPList(
            @RequestBody ZfireParamBean zfireParamBean
    ) {
        IPage<WorkerStockAccPVO> page = workerStockLogic.workerStockAccPList(zfireParamBean);
        return ResponseHelper.success(page, new TypeReference<WorkerStockAccPVO>() {});
    }

    @PostMapping("acc/listP/export")
    @ApiOperation("配件网点库存-导出")
    public void accListPExport(
            @RequestBody ZfireParamBean zfireParamBean,
            HttpServletRequest request,
            HttpServletResponse response
    ) throws Exception {
        //2.查询要导出的内容
        IPage<WorkerStockAccPVO> page = workerStockLogic.workerStockAccPList(zfireParamBean);
        //3.导出
        FieldUtils.exportData(page.getRecords(), zfireParamBean.getExportFields(), request, response);
    }

}
