package com.gree.mall.manager.controller.trade;

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.trade.OldProductManagerDetail;
import com.gree.mall.manager.bean.order.OldProductManagerAdd;
import com.gree.mall.manager.bean.trade.OldProductManagerVO;
import com.gree.mall.manager.helper.ResponseHelper;
import com.gree.mall.manager.logic.trade.OldProdctManagerLogic;
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.web.bind.annotation.*;

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

@Slf4j
@RestController
@Api(value = "旧机管理API", tags = {"旧机管理API"})
@RequestMapping(value = "/oldManager", produces = "application/json; charset=utf-8")
public class OldProductManagerController {

    @Autowired
    OldProdctManagerLogic oldProdctManagerLogic;




    @ZfireList
    @PostMapping("/list/page")
    @ApiOperation(value = "旧机管理列表")
    public ResponseHelper<IPage<OldProductManagerVO>> pageV2(
            @RequestBody ZfireParamBean zfireParamBean
    ) {
        IPage<OldProductManagerVO> page = oldProdctManagerLogic.pageV2(zfireParamBean);
        return ResponseHelper.success(page, new TypeReference<OldProductManagerVO>() {});
    }

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

    @PostMapping("/add")
    @ApiOperation(value = "旧机管理新增")
    public ResponseHelper addItem(
            @ApiParam(value = "object",required = true) @RequestBody OldProductManagerAdd oldProductManagerAdd, HttpServletRequest request) {
        oldProdctManagerLogic.addItem(oldProductManagerAdd);
        return ResponseHelper.success();
    }

    @PostMapping("/update")
    @ApiOperation(value = "修改旧机管理")
    public ResponseHelper update(
            @ApiParam(value = "object",required = true) @RequestBody OldProductManagerAdd oldProductManagerAdd, HttpServletRequest request
    ) {
        oldProdctManagerLogic.update(oldProductManagerAdd,request);
        return ResponseHelper.success();
    }

    @PostMapping("/del")
    @ApiOperation(value = "删除")
    public ResponseHelper del(
            @ApiParam(value = "id",required = true) @RequestParam String oldManagerId
    ) {
        oldProdctManagerLogic.del(oldManagerId);
        return ResponseHelper.success();
    }

    @PostMapping("/out")
    @ApiOperation(value = "出库")
    public ResponseHelper out(
            @ApiParam(value = "id",required = true) @RequestParam List<String> oldManagerId
    ) {
        oldProdctManagerLogic.out(oldManagerId);
        return ResponseHelper.success();
    }

    @PostMapping("/detail")
    @ApiOperation(value = "详情")
    public ResponseHelper detail(
            @ApiParam(value = "id",required = true) @RequestParam String oldManagerId
    ) {
        OldProductManagerDetail oldProductManagerDetail = oldProdctManagerLogic.detail(oldManagerId);
        return ResponseHelper.success(oldProductManagerDetail);
    }

}
