package com.ghy.web.controller.order;

import java.util.List;

import org.apache.ibatis.annotations.Param;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.ghy.common.annotation.Log;
import com.ghy.common.enums.BusinessType;
import com.ghy.order.domain.OrderAttachmentRecord;
import com.ghy.order.service.IOrderAttachmentRecordService;
import com.ghy.common.core.controller.BaseController;
import com.ghy.common.core.domain.AjaxResult;
import com.ghy.common.utils.poi.ExcelUtil;
import com.ghy.common.core.page.TableDataInfo;

/**
 * 附件费Controller
 *
 * @author clunt
 * @date 2024-01-02
 */
@Controller
@RequestMapping("/order/attach")
public class OrderAttachmentRecordController extends BaseController
{
    private String prefix = "order/attach";

    @Autowired
    private IOrderAttachmentRecordService orderAttachmentRecordService;

    @RequiresPermissions("attach:record:view")
    @GetMapping()
    public String record()
    {
        return prefix + "/record";
    }

    /**
     * 查询附件费列表
     */
    @RequiresPermissions("attach:record:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(OrderAttachmentRecord orderAttachmentRecord)
    {
        startPage();
        List<OrderAttachmentRecord> list = orderAttachmentRecordService.selectOrderAttachmentRecordList(orderAttachmentRecord);
        return getDataTable(list);
    }

    /**
     * App查询附件费列表
     */
    @PostMapping("/app/list")
    @ResponseBody
    public TableDataInfo appList(@RequestBody OrderAttachmentRecord orderAttachmentRecord)
    {
        startPage();
        List<OrderAttachmentRecord> list = orderAttachmentRecordService.selectOrderAttachmentRecordList(orderAttachmentRecord);
        return getDataTable(list);
    }


    /**
     * 导出附件费列表
     */
    @RequiresPermissions("attach:record:export")
    @Log(title = "附件费", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(OrderAttachmentRecord orderAttachmentRecord)
    {
        List<OrderAttachmentRecord> list = orderAttachmentRecordService.selectOrderAttachmentRecordList(orderAttachmentRecord);
        ExcelUtil<OrderAttachmentRecord> util = new ExcelUtil<OrderAttachmentRecord>(OrderAttachmentRecord.class);
        return util.exportExcel(list, "附件费数据");
    }

    /**
     * 新增附件费
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存附件费
     */
    @RequiresPermissions("attach:record:add")
    @Log(title = "附件费", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(OrderAttachmentRecord orderAttachmentRecord)
    {
        return toAjax(orderAttachmentRecordService.insertOrderAttachmentRecord(orderAttachmentRecord));
    }

    @PostMapping("/appAdd")
    @ResponseBody
    public AjaxResult appAdd(OrderAttachmentRecord orderAttachmentRecord)
    {
        return toAjax(orderAttachmentRecordService.insertOrderAttachmentRecord(orderAttachmentRecord));
    }

    /**
     * 修改附件费
     */
    @RequiresPermissions("attach:record:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        OrderAttachmentRecord orderAttachmentRecord = orderAttachmentRecordService.selectOrderAttachmentRecordById(id);
        mmap.put("orderAttachmentRecord", orderAttachmentRecord);
        return prefix + "/edit";
    }

    /**
     * 修改保存附件费
     */
    @RequiresPermissions("attach:record:edit")
    @Log(title = "附件费", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(OrderAttachmentRecord orderAttachmentRecord)
    {
        return toAjax(orderAttachmentRecordService.updateOrderAttachmentRecord(orderAttachmentRecord));
    }

    /**
     * 删除附件费
     */
    @RequiresPermissions("attach:record:remove")
    @Log(title = "附件费", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(orderAttachmentRecordService.deleteOrderAttachmentRecordByIds(ids));
    }



    /**
     * 删除附件费 根据子单id
     */
    @PostMapping( "/deleteByDetailId")
    @ResponseBody
    public AjaxResult deleteByDetailId(@RequestParam Long orderDetailId)
    {
        return toAjax(orderAttachmentRecordService.deleteOrderAttachmentRecordByOrderDetailId(orderDetailId));
    }
}