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

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.goods.*;
import com.gree.mall.manager.exception.RemoteServiceException;
import com.gree.mall.manager.helper.ResponseHelper;
import com.gree.mall.manager.logic.goods.GoodsApplyLogic;
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 = "/goodsApply", produces = "application/json; charset=utf-8")
public class GoodsApplyController {

    @Resource
    GoodsApplyLogic goodsApplyLogic;


    @ZfireList
    @PostMapping("/list")
    @ApiOperation(value = "价格调整申请列表")
    public ResponseHelper<IPage<GoodsApplyVO>> list(
            @RequestBody ZfireParamBean zfireParamBean
    ) throws RemoteServiceException {
        ZfireParamBean zfireParam = FieldUtils.supplyParam(zfireParamBean);
        IPage<GoodsApplyVO> adminDeptVOIPage = goodsApplyLogic.applyList(new Page(zfireParam.getPageNum(), zfireParam.getPageSize()), zfireParam);
        return ResponseHelper.success(adminDeptVOIPage, new TypeReference<GoodsApplyVO>() {});
    }

    @PostMapping("/list/export")
    @ApiOperation(value = "价格调整申请列表导出")
    public void listExport(
            @RequestBody ZfireParamBean zfireParamBean,
            HttpServletRequest request,
            HttpServletResponse response
    ) throws Exception {
        ZfireParamBean zfireParam = FieldUtils.supplyParam(zfireParamBean);
        IPage<GoodsApplyVO> adminDeptVOIPage = goodsApplyLogic.applyList(new Page(zfireParam.getPageNum(), zfireParam.getPageSize()), zfireParam);
        //3.导出
        FieldUtils.exportData(adminDeptVOIPage.getRecords(), zfireParam.getExportFields(), request, response);
    }

    @PostMapping("/count")
    @ApiOperation(value = "统计")
    public ResponseHelper<GoodsApplyCount> count(
            HttpServletRequest request
    ) throws RemoteServiceException {
        GoodsApplyCount  goodsApplyCount= goodsApplyLogic.countNum();
        return ResponseHelper.success(goodsApplyCount);
    }

    @PostMapping("/detail")
    @ApiOperation(value = "价格申请详情")
    public ResponseHelper<GoodsApplyDetail> listbycategory(
            @ApiParam(value = "申请id") @RequestParam(required = false) String goodsApplyId,
            HttpServletRequest request
    ) throws RemoteServiceException {
        GoodsApplyDetail  goodsApplyDetails= goodsApplyLogic.detail(goodsApplyId,request);
        return ResponseHelper.success(goodsApplyDetails);
    }


    @PostMapping("/add")
    @ApiOperation(value = "价格申请新增")
    public ResponseHelper<GoodsApplyDetail> add(
            @RequestBody GoodsApplyAddUp goodsApplyAddUp,
            HttpServletRequest request
    ) throws RemoteServiceException {
        goodsApplyLogic.add(goodsApplyAddUp,request);
        return ResponseHelper.success();
    }

    @PostMapping("/update")
    @ApiOperation(value = "价格申请修改")
    public ResponseHelper<GoodsApplyDetail> update(
            @RequestBody GoodsApplyAddUp goodsApplyAddUp,
            HttpServletRequest request
    ) throws RemoteServiceException {
        goodsApplyLogic.update(goodsApplyAddUp,request);
        return ResponseHelper.success();
    }


    @PostMapping("/approve")
    @ApiOperation(value = "审批")
    public ResponseHelper<GoodsApplyDetail> approve(
            @ApiParam(value = "申请id") @RequestParam(required = false) String goodsApplyId,
            @ApiParam(value = "审批备注") @RequestParam(required = false) String approveRemark,
            @ApiParam(value = "WAIT 待审核 OK 通过  FAIL 驳回") @RequestParam(required = false) String status,
            HttpServletRequest request
    ) throws RemoteServiceException {
        goodsApplyLogic.approve(goodsApplyId,status,approveRemark);
        return ResponseHelper.success();
    }
}
