package com.gree.mall.manager.controller;

import cn.hutool.core.lang.TypeReference;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gree.mall.manager.annotation.ZfireList;
import com.gree.mall.manager.bean.StorageVO;
import com.gree.mall.manager.exception.RemoteServiceException;
import com.gree.mall.manager.helper.ResponseHelper;
import com.gree.mall.manager.logic.StorageLogic;
import com.gree.mall.manager.plus.entity.Storage;
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.web.bind.annotation.*;

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

@Slf4j
@RestController
@Api(value = "仓储管理", tags ={"仓储管理"} )
@RequestMapping(value = "/storage", produces = "application/json; charset=utf-8")
public class StorageController {

    @Resource
    StorageLogic storageLogic;

    @ZfireList
    @PostMapping("/list/pageV2")
    @ApiOperation(value = "仓储列表V2")
    public ResponseHelper<IPage<StorageVO>> pageV2(
            @RequestBody ZfireParamBean zfireParamBean
    ) {
        IPage<StorageVO> page = storageLogic.pageV2(zfireParamBean);
        return ResponseHelper.success(page, new TypeReference<StorageVO>() {});
    }

    @PostMapping("/pageExport")
    @ApiOperation(value = "仓储列表V2导出")
    public void listExport(@RequestBody ZfireParamBean zfireParamBean, HttpServletRequest request, HttpServletResponse response) throws Exception {
        //1.组装查询条件
        ZfireParamBean zfireParam = FieldUtils.supplyParam(zfireParamBean);
        //2.查询要导出的内容
        IPage<StorageVO> page = storageLogic.pageV2(zfireParamBean);
        //3.导出
        FieldUtils.exportData(page.getRecords(), zfireParam.getExportFields(), request, response);
    }


    @GetMapping("/list")
    @ApiOperation(value = "仓储列表")
    public ResponseHelper<Page<Storage>> page(
            @ApiParam(value = "属性", required = false) @RequestParam(required = false) String type,
            @ApiParam(value = "状态") @RequestParam(required = false) Boolean status,
            @ApiParam(value = "页号", required = true) @RequestParam Integer pageNum,
            @ApiParam(value = "页大小", required = true) @RequestParam Integer pageSize,
            HttpServletRequest request
    ) throws RemoteServiceException {
        IPage<Storage> page = storageLogic.page(type, status, pageNum, pageSize, request);
        return ResponseHelper.success(page);
    }

    @GetMapping("/detail")
    @ApiOperation(value = "仓储详情")
    public ResponseHelper<Storage> detail(
            @ApiParam(value = "id",required = true) @RequestParam() String id
    ) throws RemoteServiceException {
        Storage detail = storageLogic.detail(id);
        return ResponseHelper.success(detail);
    }


    @PostMapping("/add")
    @ApiOperation(value = "新增仓储")
    public ResponseHelper add(
            HttpServletRequest request,
            @ApiParam(value = "object",required = true) @RequestBody Storage storage) {
        storageLogic.add(request,storage);
        return ResponseHelper.success();
    }

    @PostMapping("/update")
    @ApiOperation(value = "修改仓储")
    public ResponseHelper update(
            @ApiParam(value = "object",required = true) @RequestBody Storage storage) {
        storageLogic.update(storage);
        return ResponseHelper.success();
    }

    @PostMapping("/delete")
    @ApiOperation(value = "删除仓储")
    public ResponseHelper delete(
            @ApiParam(value = "仓储id",required = true) @RequestParam String storageId
    ) {
        storageLogic.delete(storageId);
        return ResponseHelper.success();
    }

    @PostMapping("/batch/update/status")
    @ApiOperation(value = "仓储管理-批量更新状态")
    public ResponseHelper batchUpdateStatus(
            @ApiParam(value = "id", required = true) @RequestParam List<String> ids,
            @ApiParam(value = "状态 true=开启 false=关闭", required = true) @RequestParam Boolean status
    ) throws Exception {
        storageLogic.batchUpdateStatus(ids, status);
        return ResponseHelper.success();
    }

}
