package com.ruoyi.web.controller.shipping.controller;

import java.util.List;

import com.ruoyi.common.utils.StringUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.shipping.domain.TCtnprice;
import com.ruoyi.shipping.service.ITCtnpriceService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 海运运价Controller
 * 
 * @author ruoyi
 * @date 2021-04-06
 */
@RestController
@RequestMapping("/shipping/ctnprice")
public class TCtnpriceController extends BaseController
{
    @Autowired
    private ITCtnpriceService tCtnpriceService;

    /**
     * 查询海运运价列表
     */
    @PreAuthorize("@ss.hasPermi('shipping:ctnprice:list')")
    @GetMapping("/list")
    public TableDataInfo list(TCtnprice tCtnprice)
    {
        startPage();
        List<TCtnprice> list = tCtnpriceService.selectTCtnpriceList(tCtnprice);
        return getDataTable(list);
    }

    /**
     * 导出海运运价列表
     */
    @PreAuthorize("@ss.hasPermi('shipping:ctnprice:export')")
    @Log(title = "海运运价", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(TCtnprice tCtnprice)
    {
        List<TCtnprice> list = tCtnpriceService.selectTCtnpriceList(tCtnprice);
        ExcelUtil<TCtnprice> util = new ExcelUtil<TCtnprice>(TCtnprice.class);
        return util.exportExcel(list, "ctnprice");
    }

    /**
     * 获取海运运价详细信息
     */
    @PreAuthorize("@ss.hasPermi('shipping:ctnprice:query')")
    @GetMapping(value = "/{fId}")
    public AjaxResult getInfo(@PathVariable("fId") Long fId)
    {
        return tCtnpriceService.selectTCtnprice(fId);
    }

    /**
     * 新增海运运价
     */
    //@PreAuthorize("@ss.hasPermi('shipping:ctnprice:add')")
    @Log(title = "海运运价", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestParam String tCtnprice,
                          @RequestParam String tCtnpriceItems)
    {
        if (StringUtils.isEmpty(tCtnprice)){
            return AjaxResult.error("未找到运价主表信息,请确认");
        }
        if (StringUtils.isEmpty(tCtnpriceItems) || "[]".equals(tCtnpriceItems)){
            return AjaxResult.error("未找到运价详细表数据,请确认");
        }
        return tCtnpriceService.insertTCtnpriceNew(tCtnprice,tCtnpriceItems);
    }

    /**
     * 修改海运运价
     */
    @PreAuthorize("@ss.hasPermi('shipping:ctnprice:edit')")
    @Log(title = "海运运价", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TCtnprice tCtnprice)
    {
        if (tCtnprice.getfStatus().equals("T") ||tCtnprice.getfStatus().equals("正常")){
            tCtnprice.setfStatus("T");
        }else {
            tCtnprice.setfStatus("F");
        }
        return toAjax(tCtnpriceService.updateTCtnprice(tCtnprice));
    }

    /**
     * 删除海运运价
     */
    @PreAuthorize("@ss.hasPermi('shipping:ctnprice:remove')")
    @Log(title = "海运运价", businessType = BusinessType.DELETE)
	@DeleteMapping("/{fIds}")
    public AjaxResult remove(@PathVariable Long[] fIds)
    {
        return toAjax(tCtnpriceService.deleteTCtnpriceByIds(fIds));
    }

    /**
     * 网页版海运运价查询
     * @param tCtnprice
     * @return
     */
    @GetMapping("/freightSelect")
    public TableDataInfo freightSelect(TCtnprice tCtnprice)
    {
        startPage();
        List<TCtnprice> list = tCtnpriceService.freightSelect(tCtnprice);
        return getDataTable(list);
    }
}
