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.core.domain.model.LoginUser;
import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.spring.SpringUtils;
import com.ruoyi.framework.web.service.TokenService;
import com.ruoyi.shipping.excel.AirForceStationExcel;
import com.ruoyi.shipping.excel.BoxDistributionStatistics;
import com.ruoyi.shipping.excel.TCntrnoExcel;
import com.ruoyi.shipping.service.ITCntrService;
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.TCntrno;
import com.ruoyi.shipping.service.ITCntrnoService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;


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

    /**
     * 查询集装箱号列表
     */
    @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.selectTcntrnoMessage(tCntrno);
        ExcelUtil<TCntrno> util = new ExcelUtil<TCntrno>(TCntrno.class);
        return util.exportExcel(list, "箱信息");
    }

    /**
     * 获取集装箱号详细信息
     */
    @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.deleteTCntrnoByIds(fIds));
    }
    /**
     * 查询集装箱信息
     */
    @PreAuthorize("@ss.hasPermi('shipping:cntrno:getTCntrnoMessage')")
    @GetMapping("/getTCntrnoMessage")
    public TableDataInfo getTCntrnoMessage(TCntrno tCntrno)
    {
        List<String> stringList = itCntrService.selectTCntrFNo();
        startPage();
        List<Map<String, Object>> list = tCntrnoService.getTCntrnoMessage(tCntrno,stringList);
        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);
    }
    /**
     * 导入集装箱动态数据
     */
    @PreAuthorize("@ss.hasPermi('shipping:cntrno:import')")
    @Log(title = "导入集装箱动态", businessType = BusinessType.IMPORT)
    @PostMapping("/importDataTcntrno")
    public AjaxResult importDataTcntrno(@RequestParam("fileName") MultipartFile fileName,@RequestParam("fileName") boolean updateSupport) throws Exception
    {
        if (fileName.isEmpty()) {
            return AjaxResult.error("该文件无内容！");
        }
        // 如果传入false为不覆盖
        AjaxResult ajaxResult= null;
        if(updateSupport==false){
            ajaxResult= tCntrnoService.importTcntrnoDataExcel(fileName);
        }else{
            ajaxResult= tCntrnoService.importUpdateDataTcntrnoExcel(fileName);
        }
        return AjaxResult.success(ajaxResult);
    }

    /**
     * 下载箱信息导入模板
     */
    @GetMapping("/importTCntrnoTemplate")
    public AjaxResult importTCntrnoTemplate()
    {
        ExcelUtil<TCntrnoExcel> util = new ExcelUtil<TCntrnoExcel>(TCntrnoExcel.class);
        return util.importTemplateExcel("箱信息数据模板");
    }
    /**
     * 导入【箱信息】Excel
     */
    @PreAuthorize("@ss.hasPermi('shipping:cntrno:import')")
    //@Log(title = "导入集装箱动态", businessType = BusinessType.IMPORT)
    @PostMapping(value = "/importCreate")
    public AjaxResult importCreateCompanyMsg(@RequestParam("file") MultipartFile file)throws Exception
    {
        return tCntrnoService.importCreateCompanyMsg(file);
    }
    /**
     * 批量导入箱数据
     */
    //@PreAuthorize("@ss.hasPermi('shipping:cntrno:add')")
    @Log(title = "集装箱号", businessType = BusinessType.INSERT)
    @PostMapping("/batch")
    public AjaxResult batch(@RequestParam("tCntrno") String tCntrno)
    {
        if (StringUtils.isEmpty(tCntrno)){
            return AjaxResult.error("导入数据不能为空");
        }else {
            LoginUser loginUser = SpringUtils.getBean(TokenService.class).getLoginUser(ServletUtils.getRequest());
            return tCntrnoService.batch(tCntrno,loginUser);
        }
    }

    /**
     * 商务端获取集装箱 箱号、地点、空重
     * @param tCntrno
     * @return
     */
    @GetMapping("/getTcntrnoFno")
    public TableDataInfo getTcntrnoFno(TCntrno tCntrno)
    {
        startPage();
        List<Map<String, Object>> list = tCntrnoService.getTcntrnoFno(tCntrno);
        return getDataTable(list);
    }
    /**
     * 下载场站箱动态模板
     */
    @GetMapping("/importTCnTrNoAirForce")
    public AjaxResult importTCnTrNoAirForce()
    {
        ExcelUtil<AirForceStationExcel> util = new ExcelUtil<AirForceStationExcel>(AirForceStationExcel.class);
        return util.importTemplateExcel("场站箱动态模板");
    }
    /**
     * 凯和导入场站箱动态
     * @param file
     * @return
     * @throws Exception
     */
    @PostMapping(value = "/importAirForceStation")
    public AjaxResult importAirForceStation(@RequestParam("file") MultipartFile file)
    {
        try {
            return tCntrnoService.importAirForceStation(file);
        } catch (Exception e) {
            return AjaxResult.error("导入箱站动态失败");
        }
    }
    /**
     * 保存场站动态信息
     */
    //@PreAuthorize("@ss.hasPermi('shipping:cntrno:updateTCntrno')")
    //@Log(title = "集装箱号", businessType = BusinessType.UPDATE)
    @PostMapping("/updateAirForceStation")
    public AjaxResult updateAirForceStation(@RequestBody TCntrno tCntrno)
    {
        if (CollectionUtils.isEmpty(tCntrno.gettCntrno())){
            return AjaxResult.error("请选择后再提交");
        }
        return tCntrnoService.updateAirForceStation(tCntrno.gettCntrno());
    }

    /**
     * 箱分布统计
     * @param
     * @return
     */
    @GetMapping("/boxDistributedStatistics")
    public TableDataInfo boxDistributedStatistics(){
        startPage();
        List<BoxDistributionStatistics> list = tCntrnoService.boxDistributedStatistics();
        return getDataTable(list);
    }



}




