package com.fuint.module.backendApi.controller;

import com.fuint.common.dto.MtPurchaseOrderDto;
import com.fuint.common.vo.MtPurchaseOrderVo;
import com.fuint.framework.web.BaseController;
import com.fuint.framework.web.PageResult;
import com.fuint.framework.web.ResponseObject;
import com.fuint.common.service.PurchaseOrderService;
import com.fuint.framework.exception.BusinessCheckException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;

/**
 * 采购订单表管理类controller
 *
 * Created by huangwen
 * CopyRight https://www.fuint.cn
 */
@Api(tags="管理端-采购订单表相关接口")
@RestController
@AllArgsConstructor
@RequestMapping(value = "/backendApi/purchase_order")
public class BackendPurchaseOrderController extends BaseController {

    /**
     * 采购订单表服务接口
     */
    private PurchaseOrderService purchaseOrderService;

    /**
     * 采购订单表列表查询
     *
     * @param  request HttpServletRequest对象
     * @return 采购订单表列表
     */
    @ApiOperation(value = "采购订单表列表查询")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @CrossOrigin
    @PreAuthorize("@pms.hasPermission('purchase_order:list')")
    public ResponseObject list(HttpServletRequest request, @RequestBody MtPurchaseOrderDto mtPurchaseOrderDto) throws BusinessCheckException {

        PageResult<MtPurchaseOrderVo> pageVoList = purchaseOrderService.queryOrderPageList(mtPurchaseOrderDto);

        return getSuccessResult(pageVoList);
    }

    /**
     * 更新采购订单表状态
     *
     * @return
     */
    @ApiOperation(value = "更新采购订单表状态")
    @RequestMapping(value = "/updateStatus", method = RequestMethod.POST)
    @CrossOrigin
    @PreAuthorize("@pms.hasPermission('purchase_order:edit')")
    public ResponseObject updateStatus(HttpServletRequest request, @RequestBody MtPurchaseOrderDto mtPurchaseOrderDto) throws BusinessCheckException {
        purchaseOrderService.updatePurchaseOrderByOrderNo(mtPurchaseOrderDto);
        return getSuccessResult(true);
    }

    /**
     * 保存采购订单表
     *
     * @param request HttpServletRequest对象
     * @return
     */
    @ApiOperation(value = "保存采购订单表")
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    @CrossOrigin
    @PreAuthorize("@pms.hasPermission('purchase_order:add')")
    public ResponseObject saveHandler(HttpServletRequest request, @RequestBody MtPurchaseOrderDto mtPurchaseOrderDto) throws BusinessCheckException {
        purchaseOrderService.savePurcahseOrder(mtPurchaseOrderDto);
        return getSuccessResult(true);
    }

    /**
     * 获取采购订单表详情
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "获取采购订单表详情")
    @RequestMapping(value = "/info/{id}", method = RequestMethod.GET)
    @CrossOrigin
    @PreAuthorize("@pms.hasPermission('purchase_order:list')")
    public ResponseObject info(HttpServletRequest request, @PathVariable("id") Long id) throws BusinessCheckException {

        MtPurchaseOrderVo vo = purchaseOrderService.queryPurchaseOrderById(id);

        return getSuccessResult(vo);
    }

    @ApiOperation(value = "删除采购订单表")
    @RequestMapping(value = "/delete/{orderNo}", method = RequestMethod.GET)
    @CrossOrigin
    @PreAuthorize("@pms.hasPermission('purchase_order:delete')")
    public ResponseObject delete(HttpServletRequest request, @PathVariable("orderNo") Long orderNo) throws BusinessCheckException {

        purchaseOrderService.deletePurchaseOrderByOrderNo(orderNo);

        return getSuccessResult(true);
    }
}
