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

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

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
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.TCntrno;
import com.ruoyi.shipping.service.ITCntrnoService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 集装箱号Controller
 * 
 * @author ruoyi
 * @date 2021-03-22
 */
@RestController
@RequestMapping("/shipping/cntrno")
public class TCntrnoController extends BaseController
{
    @Autowired
    private ITCntrnoService tCntrnoService;

    /**
     * 查询集装箱号列表
     */
    @PreAuthorize("@ss.hasPermi('shipping:cntrno:selectTcntrnoMessage')")
    @GetMapping("/selectTcntrnoMessage")
    public TableDataInfo selectTcntrnoMessage(TCntrno tCntrno)
    {
        startPage();
        List<TCntrno> list = tCntrnoService.selectTcntrnoMessage(tCntrno);
        return getDataTable(list);
    }
    /**
     * 查询集装箱号列表
     */
    @PreAuthorize("@ss.hasPermi('shipping:cntrno:list')")
    @GetMapping("/list")
    public TableDataInfo list(TCntrno tCntrno)
    {
        startPage();
        List<TCntrno> list = tCntrnoService.selectTCntrnoList(tCntrno);
        return getDataTable(list);
    }

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

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

    /**
     * 新增集装箱号
     */
    @PreAuthorize("@ss.hasPermi('shipping:cntrno:add')")
    @Log(title = "集装箱号", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TCntrno tCntrno)
    {
        if (StringUtils.isEmpty(tCntrno.getfNo())){
            return AjaxResult.error("箱号不能为空");
        }
        if (tCntrno.getfTypeid() == null){
            return AjaxResult.error("箱类型不能为空");
        }
        if (StringUtils.isEmpty(tCntrno.getfOwner())){
            return AjaxResult.error("箱主不能为空");
        }
        if (StringUtils.isEmpty(tCntrno.getfUpdateaddress())){
            return AjaxResult.error("地点不能为空");
        }
        if (StringUtils.isEmpty(tCntrno.getfUpdateef())){
            return AjaxResult.error("箱空重不能为空");
        }
        if (StringUtils.isEmpty(tCntrno.getfCntrstatus())){
            return AjaxResult.error("箱状态不能为空");
        }
        if (UserConstants.NOT_UNIQUE.equals(tCntrnoService.selectTCntrnoFNo(tCntrno))) {
            return AjaxResult.error(tCntrno.getfNo() + "编号已存在");
        }
        if (tCntrno.getfId() == null){
            return toAjax(tCntrnoService.insertTCntrno(tCntrno));
        }else {
            if (tCntrno.getfStatus().equals("T") ||tCntrno.getfStatus().equals("正常")){
                tCntrno.setfStatus("T");
            }else {
                tCntrno.setfStatus("F");
            }
            return toAjax(tCntrnoService.updateTCntrnoMessage(tCntrno));
        }

    }

    /**
     * 修改集装箱号
     */
    @PreAuthorize("@ss.hasPermi('shipping:cntrno:updateTCntrno')")
    @Log(title = "集装箱号", businessType = BusinessType.UPDATE)
    @PostMapping("/updateTCntrno")
    public AjaxResult updateTCntrno(@RequestBody TCntrno tCntrno)
    {
        if (CollectionUtils.isEmpty(tCntrno.gettCntrno())){
            return AjaxResult.error("请选择后再提交");
        }
        return toAjax(tCntrnoService.updateTCntrno(tCntrno.gettCntrno()));
    }

    /**
     * 删除集装箱号将数据状态改为停用
     */
    @PreAuthorize("@ss.hasPermi('shipping:cntrno:remove')")
    @Log(title = "集装箱号", businessType = BusinessType.DELETE)
	@DeleteMapping("/{fIds}")
    public AjaxResult remove(@PathVariable Long[] fIds)
    {
        return toAjax(tCntrnoService.updateTcntrnoStatus(fIds));
    }
    /**
     * 查询集装箱信息
     */
    @PreAuthorize("@ss.hasPermi('shipping:cntrno:getTCntrnoMessage')")
    @GetMapping("/getTCntrnoMessage")
    public TableDataInfo getTCntrnoMessage(TCntrno tCntrno)
    {
        startPage();
        List<Map<String, Object>> list = tCntrnoService.getTCntrnoMessage(tCntrno);
        return getDataTable(list);
    }
    /**
     * 查询集装箱动态分布标签
     */
    //@PreAuthorize("@ss.hasPermi('shipping:cntrno:getTCntrnoChange')")
    @GetMapping("/getTCntrnoChange")
    public TableDataInfo getTCntrnoChange(TCntrno tCntrno)
    {
        startPage();
        List<String> list = tCntrnoService.getTCntrnoChange(tCntrno);
        return getDataTable(list);
    }
    /**
     * 查询集装箱动态分布
     */
    @PreAuthorize("@ss.hasPermi('shipping:cntrno:getTCntrnoDistribute')")
    @GetMapping("/getTCntrnoDistribute")
    public TableDataInfo getTCntrnoDistribute(TCntrno tCntrno)
    {
        startPage();
        List<TCntrno> list = tCntrnoService.getTCntrnoDistribute(tCntrno);
        return getDataTable(list);
    }
}
