package com.ozs.web.controller.accountmanagment;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ozs.common.annotation.Log;
import com.ozs.common.core.controller.BaseController;
import com.ozs.common.core.domain.AjaxResult;
import com.ozs.common.enums.BusinessType;
import com.ozs.common.utils.StringUtils;
import com.ozs.common.utils.file.FileUtils;
import com.ozs.common.utils.poi.ExcelUtil;
import com.ozs.entity.BaseTerminal;
import com.ozs.entity.vo.BaseVehicleTerminalVo;
import com.ozs.service.BaseTerminalService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.util.List;

/**
 * <p>
 * 机车车载终端信息表 前端控制器
 * </p>
 *
 * @author ozs
 * @since 2023-04-11
 */
@Slf4j
@RestController
@RequestMapping("/baseVehicleTerminal")
public class BaseTerminalController extends BaseController {

    @Autowired
    private BaseTerminalService baseTerminalService;

    /**
     * 车载终端信息表 分页查询
     *
     * @param baseVehicleTerminalVo
     * @return
     */
    @PostMapping("/vehicleTerminalList")
    @Log(title = "车载终端管理", businessType = BusinessType.SELECT)
    public AjaxResult vehicleTerminalList(@RequestBody BaseVehicleTerminalVo baseVehicleTerminalVo) {
        IPage<BaseTerminal> baseVehicleTerminalIPage = baseTerminalService.vehicleTerminalList(baseVehicleTerminalVo);
        return AjaxResult.success(baseVehicleTerminalIPage);
    }

    /**
     * 车载终端信息表 详情
     *
     * @param baseVehicleTerminalVo
     * @return
     */
    @PostMapping("/baseVehicleTerminalDetails")
    @Log(title = "车载终端管理", businessType = BusinessType.SELECT)
    public AjaxResult baseVehicleTerminalDetails(@RequestBody BaseVehicleTerminalVo baseVehicleTerminalVo) {
        if (StringUtils.isEmptySunhh(baseVehicleTerminalVo) || StringUtils.isEmptySunhh(baseVehicleTerminalVo.getId())) {
            return AjaxResult.error("车载终端id不能为空！");
        }
        return baseTerminalService.baseVehicleTerminalDetails(baseVehicleTerminalVo);
    }

    /**
     * 车载终端信息表 新增
     *
     * @param baseVehicleTerminal
     * @return
     */
    @PostMapping("/vehicleTerminalAdd")
    @Log(title = "车载终端管理", businessType = BusinessType.INSERT)
    public AjaxResult vehicleTerminalAdd(@RequestBody BaseTerminal baseVehicleTerminal) {
        if (StringUtils.isEmptySunhh(baseVehicleTerminal) || StringUtils.isEmptySunhh(baseVehicleTerminal.getTerminalCode())
                || StringUtils.isEmptySunhh(baseVehicleTerminal.getTerminalName())) {
            return AjaxResult.error("车载终端名称、车载终端编码不能为空！");
        }
        return baseTerminalService.vehicleTerminalAdd(baseVehicleTerminal, getUserId());
    }

    /**
     * 车载终端信息表 修改
     *
     * @param baseVehicleTerminal
     * @return
     */
    @PostMapping("/vehicleTerminalUpdate")
    @Log(title = "车载终端管理", businessType = BusinessType.UPDATE)
    public AjaxResult vehicleTerminalUpdate(@RequestBody BaseTerminal baseVehicleTerminal) {
        if (StringUtils.isEmptySunhh(baseVehicleTerminal) || StringUtils.isEmptySunhh(baseVehicleTerminal.getId())) {
            return AjaxResult.error("修改ID不能为空！");
        }
        return baseTerminalService.vehicleTerminalUpdate(baseVehicleTerminal, getUserId());
    }

    /**
     * 车载终端信息表 删除
     *
     * @param ids
     * @return
     */
    @RequestMapping("/vehicleTerminaleDelete/{ids}")
    @Log(title = "车载终端管理", businessType = BusinessType.DELETE)
    public AjaxResult vehicleDelete(@PathVariable List<Long> ids) {
        if (StringUtils.isEmptySunhh(ids)) {
            return AjaxResult.error("删除ID不能为空！");
        }
        boolean delete = baseTerminalService.removeByIds(ids);
        if (delete) {
            return AjaxResult.success();
        } else {
            return AjaxResult.error();
        }
    }

    /**
     * 车载终端信息导入
     *
     * @param file
     * @param updateSupport
     * @return
     * @throws Exception
     */
    @PostMapping("/importBaseVehicleTerminal")
    @Log(title = "车载终端管理", businessType = BusinessType.IMPORT)
    public AjaxResult importBaseVehicleTerminal(MultipartFile file, boolean updateSupport) throws Exception {
        ExcelUtil<BaseTerminal> util = new ExcelUtil<BaseTerminal>(BaseTerminal.class);
        List<BaseTerminal> accountManageList = util.importExcel(file.getInputStream());
        String message = baseTerminalService.importBaseVehicleTerminal(accountManageList, updateSupport, getUserId());
        return AjaxResult.success(message);
    }

    /**
     * 车载终端信息导出
     *
     * @param response
     */
    @PostMapping("/exportBaseVehicleTerminal")
    @Log(title = "车载终端管理", businessType = BusinessType.EXPORT)
    public void exportBaseVehicleTerminal(HttpServletResponse response, @RequestBody BaseTerminal baseVehicleTerminal) {
        List<BaseTerminal> baseVehicleList = baseTerminalService.exportBaseVehicleTerminal(baseVehicleTerminal);
        ExcelUtil<BaseTerminal> util = new ExcelUtil<>(BaseTerminal.class);
        util.exportExcel(response, baseVehicleList, "车载终端数据");
    }

    /**
     * 车载终端信息模板下载
     *
     * @param response
     */
    @GetMapping("/downloaExcel")
    @Log(title = "车载终端管理", businessType = BusinessType.OTHER)
    public void downloadZip(HttpServletResponse response) {
        try {
            InputStream resourceAsStream = this.getClass().getResourceAsStream("/template/BaseVehicleTerminal.xlsx");
            response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
            FileUtils.setAttachmentResponseHeader(response, "车载终端信息模板.xlsx");
            FileUtils.writeBytesByInput(resourceAsStream, response.getOutputStream());
        } catch (Exception e) {
            log.error("下载文件失败", e);
        }
    }

    /**
     * 终端信息表 查询
     *
     * @return
     */
    @PostMapping("/terminalAll")
    @Log(title = "终端信息表 查询", businessType = BusinessType.SELECT)
    public AjaxResult terminalAll() {
        List<BaseTerminal> list = baseTerminalService.list();
        return AjaxResult.success(list);
    }
}

