package com.xhpc.charging.station.controller;

import com.xhpc.charging.station.service.IXhpcChargingPileService;
import com.xhpc.common.core.utils.SecurityUtils;
import com.xhpc.common.core.utils.poi.ExcelUtil;
import com.xhpc.common.core.web.controller.BaseController;
import com.xhpc.common.core.web.domain.AjaxResult;
import com.xhpc.common.core.web.page.TableDataInfo;
import com.xhpc.common.domain.XhpcChargingPile;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

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

/**
 * @author yuyang
 * @date 2021/7/27 14:44
 */
@RestController
@RequestMapping("/chargingPile")
public class XhpcChargingPileController extends BaseController {

    @Autowired
    private IXhpcChargingPileService xhpcChargingPileService;

    /**
     * 桩列表
     *
     * @param serialNumber 桩编号
     * @param type         桩类型
     * @param name         桩名称
     * @return
     */
    @GetMapping("/list")
    public TableDataInfo list(String name, Integer type, String serialNumber, Long chargingStationId) {

        startPage();
        List<Map<String, Object>> list = xhpcChargingPileService.selectXhpcChargingPileList(name, type, serialNumber, chargingStationId);
        return getDataTable(list);
    }

    /**
     * 新增桩
     *
     * @return
     */
    @PostMapping(value = "/addXhpcChargingPile")
    public AjaxResult addXhpcChargingPile(@RequestBody XhpcChargingPile xhpcChargingPile) {

        return xhpcChargingPileService.addXhpcChargingPile(xhpcChargingPile);
    }

    /**
     * 编辑桩
     *
     * @return
     */
    @PostMapping(value = "/updateXhpcChargingPile")
    public AjaxResult updateXhpcChargingPile(@RequestBody XhpcChargingPile xhpcChargingPile) {

        return xhpcChargingPileService.updateXhpcChargingPile(xhpcChargingPile);
    }


    /**
     * 桩详情
     *
     * @return
     */
    @GetMapping(value = "/selectXhpcChargingPile")
    public AjaxResult selectXhpcChargingPile(@RequestParam(value = "chargingPileId") Long chargingPileId) {

        return xhpcChargingPileService.selectXhpcChargingPile(chargingPileId);
    }

    /**
     * 停用
     *
     * @return
     */
    @PostMapping(value = "/updateStatus")
    public AjaxResult updateStatus(@RequestBody XhpcChargingPile xhpcChargingPile) {

        return xhpcChargingPileService.updateStatus(xhpcChargingPile.getChargingPileId(), xhpcChargingPile.getStatus());
    }

    /**
     * 删除电站
     */
    @DeleteMapping("/{chargingPileId}")
    public AjaxResult remove(@PathVariable Long chargingPileId) {

        return toAjax(xhpcChargingPileService.updateXhpcChargingPileById(chargingPileId));
    }

    /**
     * 下载对应终端的图片
     */
    @PostMapping(value = "/downloadsTerminalImgs")
    public AjaxResult downloadsTerminalImgs(@RequestBody List<Integer> terminalIds){
       List<Map<String,Object>> xhpcImgs = xhpcChargingPileService.downloadsTerminalImgs(terminalIds);
        return AjaxResult.success(xhpcImgs);

    }

    /**
     * 导入电桩
     * @param file
     * @param updateSupport
     * @return
     * @throws Exception
     */
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception {
        ExcelUtil<XhpcChargingPile> util = new ExcelUtil<XhpcChargingPile>(XhpcChargingPile.class);
        List<XhpcChargingPile> pileList = util.importExcel(file.getInputStream());
        String operName = SecurityUtils.getUsername();
        String message = xhpcChargingPileService.importPile(pileList, updateSupport, operName);
        return AjaxResult.success(message);
    }

}
