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.TVessel;
import com.ruoyi.shipping.service.ITVesselService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 船名Controller
 * 
 * @author ruoyi
 * @date 2021-04-02
 */
@RestController
@RequestMapping("/shipping/vessel")
public class TVesselController extends BaseController
{
    @Autowired
    private ITVesselService tVesselService;

    /**
     * 查询船名列表
     */
    @PreAuthorize("@ss.hasPermi('shipping:vessel:list')")
    @GetMapping("/list")
    public TableDataInfo list(TVessel tVessel)
    {
        startPage();
        List<TVessel> list = tVesselService.selectTVesselList(tVessel);
        return getDataTable(list);
    }

    /**
     * 导出船名列表
     */
    @PreAuthorize("@ss.hasPermi('shipping:vessel:export')")
    @Log(title = "船名", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(TVessel tVessel)
    {
        List<TVessel> list = tVesselService.selectTVesselList(tVessel);
        ExcelUtil<TVessel> util = new ExcelUtil<TVessel>(TVessel.class);
        return util.exportExcel(list, "vessel");
    }

    /**
     * 获取船名详细信息
     */
    @PreAuthorize("@ss.hasPermi('shipping:vessel:query')")
    @GetMapping(value = "/{fId}")
    public AjaxResult getInfo(@PathVariable("fId") Long fId)
    {
        return AjaxResult.success(tVesselService.selectTVesselById(fId));
    }

    /**
     * 新增船名
     */
    @PreAuthorize("@ss.hasPermi('shipping:vessel:add')")
    @Log(title = "船名", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TVessel tVessel)
    {
        if (StringUtils.isEmpty(tVessel.getfNo())){
            return AjaxResult.error("编号不能为空");
        }
        if (StringUtils.isEmpty(tVessel.getfName())){
            return AjaxResult.error("名称不能为空");
        }
        if (StringUtils.isEmpty(tVessel.getfEname())){
            return AjaxResult.error("英文名不能为空");
        }
        if (StringUtils.isEmpty(tVessel.getfImo())){
            return AjaxResult.error("船舶呼号不能为空");
        }
        if (UserConstants.NOT_UNIQUE.equals(tVesselService.selectTVesselfNo(tVessel))) {
            return AjaxResult.error(tVessel.getfNo() + "编号已存在");
        } else if (UserConstants.NOT_UNIQUE.equals(tVesselService.selectTVesselfName(tVessel))){
            return AjaxResult.error(tVessel.getfName() + "名称已存在");
        } else if (UserConstants.NOT_UNIQUE.equals(tVesselService.selectTVesselfEname(tVessel))){
            return AjaxResult.error(tVessel.getfEname() + "英文名称已存在");
        } else if (UserConstants.NOT_UNIQUE.equals(tVesselService.selectTVesselfImo(tVessel))){
            return AjaxResult.error(tVessel.getfImo() + "船舶呼号已存在");
        }
        if (tVessel.getfId() == null){
            return toAjax(tVesselService.insertTVessel(tVessel));
        }else {
            if (tVessel.getfStatus().equals("T") ||tVessel.getfStatus().equals("正常")){
                tVessel.setfStatus("T");
            }else {
                tVessel.setfStatus("F");
            }
            return toAjax(tVesselService.updateTVessel(tVessel));
        }

    }

    /**
     * 修改船名
     */
    @PreAuthorize("@ss.hasPermi('shipping:vessel:edit')")
    @Log(title = "船名", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TVessel tVessel)
    {
        if (tVessel.getfStatus().equals("T") ||tVessel.getfStatus().equals("正常")){
            tVessel.setfStatus("T");
        }else {
            tVessel.setfStatus("F");
        }
        return toAjax(tVesselService.updateTVessel(tVessel));
    }

    /**
     * 删除船名
     */
    @PreAuthorize("@ss.hasPermi('shipping:vessel:remove')")
    @Log(title = "船名", businessType = BusinessType.DELETE)
	@DeleteMapping("/{fIds}")
    public AjaxResult remove(@PathVariable Long[] fIds)
    {
        return toAjax(tVesselService.deleteTVesselByIds(fIds));
    }
    /**
     * 查询船名
     */
    //@PreAuthorize("@ss.hasPermi('shipping:vessel:selectTVesselfNumber')")
    @GetMapping("/selectTVesselfNumber")
    public TableDataInfo selectTVesselfNumber(TVessel tVessel)
    {
        startPage();
        List<Map<String, Object>> list = tVesselService.selectTVesselfNumber(tVessel);
        return getDataTable(list);
    }
}
