package com.xhpc.charging.station.controller;

import com.xhpc.charging.station.service.IXhpcChargingPileService;
import com.xhpc.common.api.PowerPileService;
import com.xhpc.common.core.domain.R;
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 com.xhpc.common.log.annotation.Log;
import com.xhpc.common.log.enums.BusinessType;
import com.xhpc.common.redis.service.RedisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

import static com.xhpc.common.data.redis.StaticBeanUtil.REDIS;

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

    @Autowired
    private IXhpcChargingPileService xhpcChargingPileService;
    @Autowired
    private PowerPileService powerPileService;
    @Autowired
    RedisService redisService;

    /**
     * 桩列表
     *
     * @param serialNumber 桩编号
     * @param type         桩类型
     * @param name         桩名称
     * @return
     */
    @GetMapping("/list")
    public TableDataInfo list(HttpServletRequest request,String tenantId,Long operatorId,String name, Integer type, String serialNumber, Long chargingStationId) {
        List<Map<String, Object>> list = xhpcChargingPileService.selectXhpcChargingPileList(request,tenantId,operatorId,name, type, serialNumber, chargingStationId);
        return getDataTable(list);
    }

    /**
     * 新增桩
     *
     * @return
     */
   // @Log(title = "场站-添加桩", businessType = BusinessType.INSERT)
    @PostMapping(value = "/addXhpcChargingPile")
    public AjaxResult addXhpcChargingPile(@RequestBody XhpcChargingPile xhpcChargingPile) {

        return xhpcChargingPileService.addXhpcChargingPile(xhpcChargingPile);
    }

    /**
     * 编辑桩
     *
     * @return
     */
    @Log(title = "场站-修改桩", businessType = BusinessType.UPDATE)
    @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
     */
    @Log(title = "桩-停用", businessType = BusinessType.UPDATE)
    @PostMapping(value = "/updateStatus")
    public AjaxResult updateStatus(@RequestBody XhpcChargingPile xhpcChargingPile) {
        if(xhpcChargingPile.getStatus()==1){
            if(null==xhpcChargingPile.getPrompt() || xhpcChargingPile.getPrompt().length()>50){
                return AjaxResult.error("桩提示语为空且50字之内");
            }
        }
        return xhpcChargingPileService.updateStatus(xhpcChargingPile.getChargingPileId(), xhpcChargingPile.getStatus(),xhpcChargingPile.getPrompt());
    }

    /**
     * 桩-删除
     */
    @Log(title = "桩-删除", businessType = BusinessType.DELETE)
    @DeleteMapping("/{chargingPileId}")
    public AjaxResult remove(@PathVariable Long chargingPileId) {

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

    /**
     * 下载对应终端的图片
     */
    @Log(title = "桩-下载对应终端的图片", businessType = BusinessType.EXPORT)
    @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
     */
    @Log(title = "桩-导入电桩", businessType = BusinessType.IMPORT)
    @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);
    }

    /**
     * generate all qrcode by id of charging station and id of charging pile
     *
     * @author WH
     * @date 2022/2/17 10:14
     * @since version-1.0
     */
    @GetMapping("/all/qr-code")
    public AjaxResult generateQrCode(@RequestParam Long chargingStationId, @RequestParam(required = false) Long pileId) {

        return xhpcChargingPileService.generateQrCode(chargingStationId, pileId);
    }


    //下发校时校价
    @GetMapping("/configRmReply")
    public AjaxResult configRmReply(String serialNumber) {

        if(serialNumber==null || "".equals(serialNumber)){
            return AjaxResult.error("桩号为空");
        }
        R r = powerPileService.configTimeNRateModel(serialNumber);
        if(r.getCode() !=200){
            return AjaxResult.error(r.getMsg());
        }
        return AjaxResult.success(r.getMsg());
    }

    // 更新桩程序
    @GetMapping("/softwareUpgrade")
    public AjaxResult softwareUpgrade(String serialNumber) {

        if(serialNumber==null || "".equals(serialNumber)){
            return AjaxResult.error("桩号为空");
        }
        R r = powerPileService.pileSoftwareUpgrade(serialNumber);
        if(r.getCode() !=200){
            return AjaxResult.error(r.getMsg());
        }
        return AjaxResult.success(r.getMsg());
    }


    // 远程重启桩
    @GetMapping("/reboot")
    public AjaxResult pileReboot(String serialNumber) {

        if(serialNumber==null || "".equals(serialNumber)){
            return AjaxResult.error("桩号为空");
        }
        R r = powerPileService.pileReboot(serialNumber);
        if(r.getCode() !=200){
            return AjaxResult.error(r.getMsg());
        }
        return AjaxResult.success(r.getMsg());
    }


    @GetMapping("/configRmReplyMessage")
    public AjaxResult configRmReplyMessage(String serialNumber) {
        if(serialNumber==null || "".equals(serialNumber)){
            return AjaxResult.error("桩号为空");
        }
        String configRateReply = redisService.getCacheObject("pile:" + serialNumber + ".configRateReply");
        if(configRateReply==null || "".equals(configRateReply)){
            return AjaxResult.success("未下发");
        }
        String configTimeReply = redisService.getCacheObject("pile:" + serialNumber + ".configTimeReply");
        if(configTimeReply !=null && !"".equals(configTimeReply)){
            return AjaxResult.success(configRateReply+"--"+configTimeReply);
        }
        return AjaxResult.success(configRateReply);
    }



    @GetMapping("/getChargingPileSerialNumber")
    public R getChargingPileSerialNumber(String serialNumber){

        return xhpcChargingPileService.getChargingPileSerialNumber(serialNumber);
    }

    //充电桩工作参数设置
    @GetMapping("/configSettings")
    public AjaxResult configSettings(String serialNumber,Integer powerNumber) {

        if(serialNumber==null || "".equals(serialNumber)){
            return AjaxResult.error("桩号为空");
        }
        if(powerNumber==null){
            return AjaxResult.error("充电桩最大允许输出功率为空");
        }
        if(powerNumber<1 || powerNumber>100){
            return AjaxResult.error("充电桩最大允许输出功率1~100之内");
        }
        redisService.setCacheObject("pile:"+serialNumber+":powerNumber",powerNumber);
        R r = powerPileService.pileConfigSettings(serialNumber,powerNumber.toString());
        if(r.getCode() !=200){
            return AjaxResult.error(r.getMsg());
        }
        return AjaxResult.success("设置成功");
    }
}
