package com.fs.oms.controller;

import java.util.List;

import com.fs.common.core.domain.R;
import com.fs.core.security.SecurityUtils;
import com.fs.oms.vo.OmsPurchaseApplyStatusVO;
import com.fs.oms.vo.OmsPurchaseApplyVO;
import com.fs.oms.vo.OmsPurchaseProductVO;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.fs.common.annotation.Log;
import com.fs.common.core.controller.BaseController;
import com.fs.common.core.domain.AjaxResult;
import com.fs.common.enums.BusinessType;
import com.fs.oms.domain.OmsPurchaseApply;
import com.fs.oms.service.IOmsPurchaseApplyService;
import com.fs.common.utils.poi.ExcelUtil;
import com.fs.common.core.page.TableDataInfo;

/**
 * 采购申请单Controller
 *
 * @author fs
 * @date 2023-06-20
 */
@RestController
@RequestMapping("/oms/apply")
public class OmsPurchaseApplyController extends BaseController
{
    @Autowired
    private IOmsPurchaseApplyService omsPurchaseApplyService;

    /**
     * 查询采购申请单列表
     */
    @PreAuthorize("@ss.hasPermi('oms:apply:list')")
    @GetMapping("/list")
    public TableDataInfo list(OmsPurchaseApply omsPurchaseApply)
    {
        startPage();
        List<OmsPurchaseApplyVO> list = omsPurchaseApplyService.selectOmsPurchaseApplyList(omsPurchaseApply);
        return getDataTable(list);
    }

    /**
     * 导出采购申请单列表
     */
//    @PreAuthorize("@ss.hasPermi('oms:apply:export')")
//    @Log(title = "采购申请单", businessType = BusinessType.EXPORT)
//    @GetMapping("/export")
//    public AjaxResult export(OmsPurchaseApply omsPurchaseApply)
//    {
//        List<OmsPurchaseApply> list = omsPurchaseApplyService.selectOmsPurchaseApplyList(omsPurchaseApply);
//        ExcelUtil<OmsPurchaseApply> util = new ExcelUtil<OmsPurchaseApply>(OmsPurchaseApply.class);
//        return util.exportExcel(list, "apply");
//    }

    /**
     * 获取采购申请单详细信息
     */
    @PreAuthorize("@ss.hasPermi('oms:apply:query')")
    @GetMapping(value = "/{id}")
    public R getInfo(@PathVariable("id") Long id)
    {
        OmsPurchaseApply omsPurchaseApply = omsPurchaseApplyService.selectOmsPurchaseApplyById(id);
        List<OmsPurchaseProductVO> list = omsPurchaseApplyService.selectOmsPurchaseProductById(id);
        return R.ok().put("data",omsPurchaseApply).put("selectProucts",list);
    }

    /**
     * 新增采购申请单
     */
    @PreAuthorize("@ss.hasPermi('oms:apply:add')")
    @Log(title = "采购申请单", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody OmsPurchaseApply omsPurchaseApply)
    {
        omsPurchaseApply.setCreateUserId(SecurityUtils.getLoginUser().getUser().getUserId()); //获取用户名称
        return toAjax(omsPurchaseApplyService.insertOmsPurchaseApply(omsPurchaseApply));
    }

    /**
     * 修改采购申请单
     */
    @PreAuthorize("@ss.hasPermi('oms:apply:edit')")
    @Log(title = "采购申请单", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody OmsPurchaseApply omsPurchaseApply)
    {
        return toAjax(omsPurchaseApplyService.updateOmsPurchaseApply(omsPurchaseApply));
    }

    /**
     * 删除采购申请单
     */
    @PreAuthorize("@ss.hasPermi('oms:apply:remove')")
    @Log(title = "采购申请单", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(omsPurchaseApplyService.deleteOmsPurchaseApplyByIds(ids));
    }

    /**
     * 获取采购申请单添加商品列表
     * @param omsPurchaseProductVO
     * @return
     */
    @GetMapping("/productList")
    public TableDataInfo productList(OmsPurchaseProductVO omsPurchaseProductVO)
    {
        startPage();
        List<OmsPurchaseProductVO> list = omsPurchaseApplyService.selectOmsPurchaseProductList(omsPurchaseProductVO);
        return getDataTable(list);
    }

    @PutMapping("/status")
    public AjaxResult editStatus(@RequestBody OmsPurchaseApplyStatusVO omsPurchaseApplyStatusVO)
    {
        return toAjax(omsPurchaseApplyService.updateApplyStatus(omsPurchaseApplyStatusVO));
    }

}
