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

import java.util.List;
import java.util.Map;

import com.ruoyi.common.constant.UserConstants;
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.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.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.TCntr;
import com.ruoyi.shipping.service.ITCntrService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 集装箱Controller
 * 
 * @author ruoyi
 * @date 2021-03-22
 */
@RestController
@RequestMapping("/shipping/cntr")
public class TCntrController extends BaseController
{
    @Autowired
    private ITCntrService tCntrService;

    /**
     * 查询集装箱列表
     */
    @GetMapping("/list")
    public TableDataInfo list(TCntr tCntr)
    {
        startPage();
        List<TCntr> list = tCntrService.selectTCntrList(tCntr);
        return getDataTable(list);
    }

    /**
     * 导出集装箱列表
     */
    @PreAuthorize("@ss.hasPermi('shipping:cntr:export')")
    @Log(title = "集装箱", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(TCntr tCntr)
    {
        List<TCntr> list = tCntrService.selectTCntrList(tCntr);
        ExcelUtil<TCntr> util = new ExcelUtil<TCntr>(TCntr.class);
        return util.exportExcel(list, "cntr");
    }

    /**
     * 获取集装箱详细信息
     */
    @PreAuthorize("@ss.hasPermi('shipping:cntr:query')")
    @GetMapping(value = "/{fId}")
    public AjaxResult getInfo(@PathVariable("fId") Long fId)
    {
        return AjaxResult.success(tCntrService.selectTCntrById(fId));
    }

    /**
     * 新增集装箱
     */
    @PreAuthorize("@ss.hasPermi('shipping:cntr:add')")
    @Log(title = "集装箱", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TCntr tCntr)
    {
        if (tCntr.getfType() == null){
            return AjaxResult.error("集装箱类型不能为空");
        }else if (StringUtils.isEmpty(tCntr.getfNo())){
            return AjaxResult.error("集装箱编号不能为空");
        }else if (StringUtils.isEmpty(tCntr.getfName())){
            return AjaxResult.error("集装箱名称不能为空");
        }else if (StringUtils.isEmpty(tCntr.getfName())){
            return AjaxResult.error("集装箱英文名不能为空");
        }
        if (UserConstants.NOT_UNIQUE.equals(tCntrService.selectTcnrFno(tCntr))) {
            return AjaxResult.error(tCntr.getfNo() + "编号已存在");
        } else if (UserConstants.NOT_UNIQUE.equals(tCntrService.selectTcnrFName(tCntr))){
            return AjaxResult.error(tCntr.getfName() + "名称已存在");
        }
        if (tCntr.getfId() == null){
            return toAjax(tCntrService.insertTCntr(tCntr));
        }else {
            if (tCntr.getfStatus().equals("T") ||tCntr.getfStatus().equals("正常")){
                tCntr.setfStatus("T");
            }else {
                tCntr.setfStatus("F");
            }
            return toAjax(tCntrService.updateTCntr(tCntr));
        }

    }

    /**
     * 修改集装箱
     */
    @PreAuthorize("@ss.hasPermi('shipping:cntr:edit')")
    @Log(title = "集装箱", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TCntr tCntr)
    {
        return toAjax(tCntrService.updateTCntr(tCntr));
    }

    /**
     * 删除集装箱
     */
    @PreAuthorize("@ss.hasPermi('shipping:cntr:remove')")
    @Log(title = "集装箱", businessType = BusinessType.DELETE)
	@DeleteMapping("/{fIds}")
    public AjaxResult remove(@PathVariable Long[] fIds)
    {
        return toAjax(tCntrService.deleteTCntrByIds(fIds));
    }
    /**
     * 查询集装箱编号
     */
    //@PreAuthorize("@ss.hasPermi('shipping:cntr:selectRcntrName')")
    @GetMapping("/selectRcntrName")
    public TableDataInfo selectRcntrName(TCntr tCntr)
    {
        startPage();
        List<Map<String,Object>> list = tCntrService.selectRcntrName(tCntr);
        return getDataTable(list);
    }
}
