package com.gree.mall.miniapp.controller.material;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.gree.mall.miniapp.bean.material.MyDataCountBean;
import com.gree.mall.miniapp.bean.material.PartsApplyBean;
import com.gree.mall.miniapp.helper.ResponseHelper;
import com.gree.mall.miniapp.logic.material.PartsApplyLogic;
import com.gree.mall.miniapp.plus.entity.WebsitPartsApply;
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;

@Slf4j
@RestController
@Api(value = "配件申请API", tags ={"配件申请API"} )
@RequestMapping(value = "/material/parts/apply", produces = "application/json; charset=utf-8")
public class PartsApplyController {

    @Resource
    PartsApplyLogic partsApplyLogic;

    @PostMapping("list")
    @ApiOperation(value = "配件申请列表")
    public ResponseHelper<IPage<WebsitPartsApply>> list(
            @ApiParam(value = "状态 ING=申请中 END=到货反馈 CANCEL=取消申请") @RequestParam(required = false) String status,
            @ApiParam(value = "页号", required = true) @RequestParam Integer pageNum,
            @ApiParam(value = "页大小", required = true) @RequestParam Integer pageSize,
            HttpServletRequest request
    ) {
        IPage<WebsitPartsApply> page = partsApplyLogic.list(status, pageNum, pageSize, request);
        return ResponseHelper.success(page);
    }

    @PostMapping("count")
    @ApiOperation(value = "配件申请统计")
    public ResponseHelper<MyDataCountBean> count(HttpServletRequest request) {
        MyDataCountBean count = partsApplyLogic.count(request);
        return ResponseHelper.success(count);
    }

    @PostMapping("detail")
    @ApiOperation(value = "详情")
    public ResponseHelper<PartsApplyBean> detail(
            @ApiParam(value = "单号id") @RequestParam String id
    ) {
        PartsApplyBean bean = partsApplyLogic.detail(id);
        return ResponseHelper.success(bean);
    }

    @PostMapping("/add")
    @ApiOperation(value = "提交申请")
    public ResponseHelper<String> add(
            @RequestBody PartsApplyBean bean
    ) throws Exception {
        String id = partsApplyLogic.add(bean);
        return ResponseHelper.success(id);
    }

    @PostMapping("/cancel")
    @ApiOperation(value = "取消申请")
    public ResponseHelper cancel(
            @ApiParam(value = "单号id") @RequestParam String id
    ) throws Exception {
        partsApplyLogic.cancel(id);
        return ResponseHelper.success();
    }
}
