package com.ruoyi.web.controller.fleet.basicData;

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

import com.ruoyi.basicData.domain.TCorps;
import com.ruoyi.basicData.service.ITCorpsService;
import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.basicData.domain.FleetDriverMsg;
import com.ruoyi.basicData.service.IFleetDriverMsgService;
import com.ruoyi.system.service.ISysUserService;
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.common.core.page.TableDataInfo;

/**
 * 【司机】Controller
 * 
 * @author ruoyi
 * @date 2021-03-04
 */
@RestController
@RequestMapping("/fleet/driverMsg")
public class FleetDriverMsgController extends BaseController
{

    @Autowired
    private ITCorpsService tCorpsService;

    @Autowired
    private IFleetDriverMsgService fleetDriverMsgService;

    /**
     * 下拉框查询【司机】id、name列表
     */
    @GetMapping("/list")
    public List<TCorps> list(TCorps tCorps)
    {
        tCorps.setfTypeid("102");
        return tCorpsService.selectTCorpsList(tCorps);
    }

    /**
     * 查询【车队】列表
     */
    @PreAuthorize("@ss.hasPermi('basicdata:corps:query')")
    @GetMapping(value = "/query")
    public TableDataInfo query(TCorps tCorps)
    {
        String typeValue = "[\"102\"]";
        tCorps.setfTypeid(typeValue);
        startPage();
        List<Map<String, Object>> list = tCorpsService.selectFleetDriverMsgListMap(tCorps);
        return getDataTable(list);
    }

    /**
     * 导出【司机】列表
     */
    @PreAuthorize("@ss.hasPermi('fleet:driverMsg:export')")
    @Log(title = "【司机】", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(FleetDriverMsg fleetDriverMsg)
    {
        List<FleetDriverMsg> list = fleetDriverMsgService.selectFleetDriverMsgList(fleetDriverMsg);
        ExcelUtil<FleetDriverMsg> util = new ExcelUtil<FleetDriverMsg>(FleetDriverMsg.class);
        return util.exportExcel(list, "msg");
    }

    /**
     * 获取【司机】详细信息
     */
    @PreAuthorize("@ss.hasPermi('basicdata:corps:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(fleetDriverMsgService.selectFleetDriverMsgById(id));
    }

    /**
     * 新增【司机】
     */
    @PreAuthorize("@ss.hasPermi('basicdata:corps:add')")
    @Log(title = "【司机】", businessType = BusinessType.INSERT)
    @PostMapping(value = "/add")
    public AjaxResult add(@RequestBody TCorps tCorps)
    {
        if (StringUtils.isEmpty(tCorps.getfTel())) {
            return AjaxResult.error("请填写司机手机号");
        }
        // 检验编号、名称、简称 唯一
        if (UserConstants.NOT_UNIQUE.equals(tCorpsService.checkUFNoUnique(tCorps))) {
            return AjaxResult.error("新增信息'" + tCorps.getfNo() + "'失败，编号已存在");
        } else if (UserConstants.NOT_UNIQUE.equals(tCorpsService.checkUFNnameUnique(tCorps))) {
            return AjaxResult.error("新增信息'" + tCorps.getfName() + "'失败，名称已存在");
        } else if (UserConstants.NOT_UNIQUE.equals(tCorpsService.checkUFCnameUnique(tCorps))) {
            return AjaxResult.error("新增信息'" + tCorps.getfCname() + "'失败，简称已存在");
        }
        return fleetDriverMsgService.insertFleetDriverMsg(tCorps);
    }

    /**
     * 修改【司机】
     */
    @PreAuthorize("@ss.hasPermi('basicdata:corps:edit')")
    @Log(title = "【司机】", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody FleetDriverMsg fleetDriverMsg)
    {
        fleetDriverMsg.setCreateBy(SecurityUtils.getUsername());
        TCorps tCorps = fleetDriverMsgService.selectFleetDriverMsgById(fleetDriverMsg.getId());
        return toAjax(fleetDriverMsgService.updateFleetDriverMsg(fleetDriverMsg));
    }

    /**
     * 删除【司机】
     */
    @Log(title = "【司机】", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        if (ids.length <= 0) {
            return AjaxResult.error("未找到需要删除的条件");
        }
        return fleetDriverMsgService.deleteFleetDriverMsgByIds(ids);
    }
}
