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

/**
 * 地点基础信息（港口 码头 堆场 航线）Controller
 * 
 * @author ruoyi
 * @date 2021-03-22
 */
@RestController
@RequestMapping("/shipping/address")
public class TAddressController extends BaseController
{
    @Autowired
    private ITAddressService tAddressService;

    /**
     * 查询地点基础信息（港口 码头 堆场 航线）下拉列表
     */
    @GetMapping("/getBasicInformation")
    public List<TAddress> getBasicInformation(TAddress tAddress)
    {
        return tAddressService.selectTAddressBasicList(tAddress);
    }

    /**
     * 查询地点基础信息（港口 码头 堆场 航线）列表
     */
    @PreAuthorize("@ss.hasPermi('shipping:address:list')")
    @GetMapping("/list")
    public TableDataInfo list(TAddress tAddress)
    {
        startPage();
        List<TAddress> list = tAddressService.selectTAddressList(tAddress);
        return getDataTable(list);
    }
    @PreAuthorize("@ss.hasPermi('shipping:address:selectCarriageList')")
    @GetMapping("/selectCarriageList")
    public TableDataInfo selectCarriageList(TAddress tAddress)
    {
        startPage();
        List<TAddress> list = tAddressService.selectCarriageList(tAddress);
        return getDataTable(list);
    }

    /**
     * 查询堆场信息
     * @param tAddress
     * @return
     */
    @PreAuthorize("@ss.hasPermi('shipping:address:selectStockDump')")
    @GetMapping("/selectStockDump")
    public TableDataInfo selectStockDump(TAddress tAddress)
    {
        startPage();
        List<TAddress> list = tAddressService.selectStockDump(tAddress);
        return getDataTable(list);
    }

    /**
     * 查看码头信息
     * @param tAddress
     * @return
     */
    @PreAuthorize("@ss.hasPermi('shipping:address:selectWarfDump')")
    @GetMapping("/selectWarfDump")
    public TableDataInfo selectWarfDump(TAddress tAddress)
    {
        startPage();
        List<TAddress> list = tAddressService.selectWarfDump(tAddress);
        return getDataTable(list);
    }

    /**
     * 查询港口名称
     * @param tAddress
     * @return
     */
    //@PreAuthorize("@ss.hasPermi('shipping:address:selectPortName')")
    @GetMapping("/selectPortName")
    public TableDataInfo selectPortName(TAddress tAddress)
    {
        startPage();
        List<Map<String, Object>> list = tAddressService.selectPortName(tAddress);
        return getDataTable(list);
    }
    /**
     * 查询码头名称
     * @param tAddress
     * @return
     */
    //@PreAuthorize("@ss.hasPermi('shipping:address:selectWharfName')")
    @GetMapping("/selectWharfName")
    public TableDataInfo selectWharfName(TAddress tAddress)
    {
        startPage();
        List<Map<String, Object>> list = tAddressService.selectWharfName(tAddress);
        return getDataTable(list);
    }
    /**
     * 查询堆场名称
     * @param tAddress
     * @return
     */
    //@PreAuthorize("@ss.hasPermi('shipping:address:selectStorageName')")
    @GetMapping("/selectStorageName")
    public TableDataInfo selectStorageName(TAddress tAddress)
    {
        startPage();
        List<Map<String, Object>> list = tAddressService.selectStorageName(tAddress);
        return getDataTable(list);
    }
    /**
     * 查询航线名称
     * @param tAddress
     * @return
     */
    //@PreAuthorize("@ss.hasPermi('shipping:address:selectAirLineName')")
    @GetMapping("/selectAirLineName")
    public TableDataInfo selectAirLineName(TAddress tAddress)
    {
        startPage();
        List<Map<String, Object>> list = tAddressService.selectAirLineName(tAddress);
        return getDataTable(list);
    }
    /**
     * 查询港口信息
     * @param tAddress
     * @return
     */
    @PreAuthorize("@ss.hasPermi('shipping:address:selectPortDump')")
    @GetMapping("/selectPortDump")
    public TableDataInfo selectPortDump(TAddress tAddress)
    {
        startPage();
        List<TAddress> list = tAddressService.selectPortDump(tAddress);
        return getDataTable(list);
    }

    /**
     * 导出地点基础信息（港口 码头 堆场 航线）列表
     */
    @PreAuthorize("@ss.hasPermi('shipping:address:export')")
    @Log(title = "地点基础信息（港口 码头 堆场 航线）", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(TAddress tAddress)
    {
        List<TAddress> list = tAddressService.selectTAddressList(tAddress);
        ExcelUtil<TAddress> util = new ExcelUtil<TAddress>(TAddress.class);
        return util.exportExcel(list, "address");
    }

    /**
     * 获取地点基础信息（港口 码头 堆场 航线）详细信息
     */
    @PreAuthorize("@ss.hasPermi('shipping:address:query')")
    @GetMapping(value = "/{fId}")
    public AjaxResult getInfo(@PathVariable("fId") Long fId)
    {
        return AjaxResult.success(tAddressService.selectTAddressById(fId));
    }

    /**
     * 新增地点基础信息（港口 码头 堆场 航线）
     */
    @PreAuthorize("@ss.hasPermi('shipping:address:add')")
    @Log(title = "地点基础信息（港口 码头 堆场 航线）", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TAddress tAddress)
    {
        if (StringUtils.isEmpty(tAddress.getfTypes())){
            return AjaxResult.error("地点类型不能为空");
        }
        if (StringUtils.isEmpty(tAddress.getfNo())){
            return AjaxResult.error("地点编号不能为空");
        }
        if (StringUtils.isEmpty(tAddress.getfName())){
            return AjaxResult.error("地点名称不能为空");
        }
        if (UserConstants.NOT_UNIQUE.equals(tAddressService.selectAddressFno(tAddress))) {
            return AjaxResult.error(tAddress.getfNo() + "编号已存在");
        } else if (UserConstants.NOT_UNIQUE.equals(tAddressService.selectAddressFName(tAddress))){
            return AjaxResult.error(tAddress.getfName() + "名称已存在");
        }
        if (tAddress.getfId()==null){
            return toAjax(tAddressService.insertTAddress(tAddress));
        }else {
            if (tAddress.getfStatus().equals("T") ||tAddress.getfStatus().equals("正常")){
                tAddress.setfStatus("T");
            }else {
                tAddress.setfStatus("F");
            }
            return toAjax(tAddressService.updateTAddress(tAddress));
        }

    }

    /**
     * 修改地点基础信息（港口 码头 堆场 航线）
     */
    @PreAuthorize("@ss.hasPermi('shipping:address:edit')")
    @Log(title = "地点基础信息（港口 码头 堆场 航线）", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TAddress tAddress)
    {
        return toAjax(tAddressService.updateTAddress(tAddress));
    }

    /**
     * 删除地点基础信息（港口 码头 堆场 航线）
     */
    @PreAuthorize("@ss.hasPermi('shipping:address:remove')")
    @Log(title = "地点基础信息（港口 码头 堆场 航线）", businessType = BusinessType.DELETE)
	@DeleteMapping("/{fIds}")
    public AjaxResult remove(@PathVariable Long[] fIds)
    {
        return toAjax(tAddressService.deleteTAddressByIds(fIds));
    }
    /**
     * 获取地点基础信息（港口 码头 堆场 航线）详细信息
     */
    //@PreAuthorize("@ss.hasPermi('shipping:address:selectAddressFno')")
    @PostMapping(value = "/selectAddressFno")
    public AjaxResult selectAddressFno(@RequestBody TAddress tAddress)
    {
        if (StringUtils.isEmpty(tAddress.getfTypes()) || StringUtils.isEmpty(tAddress.getfNo())){
            return AjaxResult.error("参数不能为空");
        }
        String addressFno = tAddressService.selectAddressFno(tAddress);
        if (StringUtils.isNotEmpty(addressFno)){
            return AjaxResult.error("编号已存在");
        }else {
            return AjaxResult.success();
        }

    }
    /**
     * 获取地点基础信息（港口 码头 堆场 航线）详细信息
     */
    //@PreAuthorize("@ss.hasPermi('shipping:address:selectAddressFName')")
    @PostMapping(value = "/selectAddressFName")
    public AjaxResult getInfo(@RequestBody TAddress tAddress)
    {
        if (StringUtils.isEmpty(tAddress.getfTypes()) || StringUtils.isEmpty(tAddress.getfName())){
            return AjaxResult.error("参数不能为空");
        }
        String addressFno = tAddressService.selectAddressFName(tAddress);
        if (StringUtils.isNotEmpty(addressFno)){
            return AjaxResult.error("名称已存在");
        }else {
            return AjaxResult.success();
        }
    }
    /**
     * 查询地点基础信息（港口 码头 堆场 航线）列表
     */
    //@PreAuthorize("@ss.hasPermi('shipping:address:selectadress')")
    @GetMapping("/selectadress")
    public TableDataInfo selectadress( )
    {
        startPage();
        List<TAddress> list = tAddressService.selectadress();
        return getDataTable(list);
    }
}
