package com.xhpc.card.controller;

import com.xhpc.card.domain.*;
import com.xhpc.card.pojo.TIccardClientUsers;
import com.xhpc.card.pojo.TIccardInfo;
import com.xhpc.card.service.IXhpcCardService;
import com.xhpc.common.api.PowerPileService;
import com.xhpc.common.core.domain.R;
import com.xhpc.common.core.web.controller.BaseController;
import com.xhpc.common.core.web.domain.AjaxResult;
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 io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

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

/**
 * @author yuyang
 * @date 2022/1/20 11:33
 */
@RestController
@RequestMapping("/card")
@Api(value = "卡管理", tags = "卡管理")
public class XhpcCardController extends BaseController {

    @Autowired
    private IXhpcCardService xhpcCardService;
    @Autowired
    private PowerPileService powerPileService;
    @Autowired
    private RedisService redisService;
    /**
     * 卡启动前判断
     *
     * @param cardno       卡物理卡号
     * @param serialNumber 终端编号
     * @return
     */
    @GetMapping("/cardStartup")
    public R cardStartup(@RequestParam(value = "cardno") String cardno, @RequestParam(value = "serialNumber") String serialNumber, @RequestParam(value = "rateModelId") String rateModelId) {

        logger.info("<<<<<<<<<<<<<<<<<<<<<<<<卡启动充电判断>>>>>>>>>>>>>>>>>");
        logger.info("<<<<<<<<<<<<<<<<<<<<<<<<卡启动充电判断>>>>>>>>>>>>>>>>>");
        return xhpcCardService.cardStartup(cardno, serialNumber, rateModelId);
    }

    @PatchMapping("/recover")
    public R<Object> recoverToReportTheLossOfCard(@RequestParam("cardId") Integer cardId) {

        return xhpcCardService.recoverToReportTheLossOfCard(cardId);
    }

    @GetMapping("/devices")
    public R<ListOfAuthorizedDevices> queryDeviceList(@RequestParam Long currentPage, @RequestParam Long items) {

        if (currentPage == null || items == null) {
            return R.fail("分页参数不能为null");
        }
        return xhpcCardService.queryDeviceList(currentPage, items);
    }

    /**
     * 逻辑删除指定的卡授权设备
     *
     * @author WH
     * @date 2022/1/26 10:37
     * @since version-1.0
     */
    @DeleteMapping("/devices/{deviceId}")
    public R<Object> deleteDevice(@PathVariable("deviceId") Integer deviceId) {

        return xhpcCardService.deleteDevice(deviceId);
    }

    /**
     * 通过查询条件来查询卡列表
     *
     * @author WH
     * @date 2022/1/26 10:37
     * @since version-1.0
     */
    @GetMapping("/list")
    public R<CardList> queryCardListBy(@Validated QueryConditions queryConditions) {

        return xhpcCardService.queryCardListBy(queryConditions);
    }

    /**
     * 绑定联网卡时回显信息接口
     *
     * @author WH
     * @date 2022/1/26 10:37
     * @since version-1.0
     */
    @GetMapping("/non-bind/{cardId}")
    public R<BindCardEcho> noBindCard(@PathVariable("cardId") Long cardId) {

        if (cardId == null) {
            return R.fail("cardId不能为null");
        }
        return xhpcCardService.noBindCardInfo(cardId);
    }

    /**
     * 查询平台或运营商端各种类型用户的接口
     *
     * @author WH
     * @date 2022/2/8 10:33
     * @since version-1.0
     */
    @GetMapping("/user/all")
    public R<SearchCardResponse> searchCardUser(SearchCardUserQueryCondition queryCondition) {

        return xhpcCardService.searchCardUser(queryCondition);
    }

    /**
     * 绑定联网卡
     */
    @PostMapping("/online/bind")
    public R<Object> onlineCardBind(@RequestBody BindCardInfo bindCardInfo) {

        return xhpcCardService.onlineCardBind(bindCardInfo);
    }

    /**
     * 查看卡用户信息接口
     */
    @GetMapping("/user")
    public R<CardUserInfo> queryWholeUserInfo(@RequestParam Long cardRecordId) {

        return xhpcCardService.queryWholeUserInfo(cardRecordId);
    }

    /**
     * According tenant id of platform query that it own all grantOperators
     *
     * @author WH
     * @date 2022/2/12 15:12
     * @since version-1.0
     */
    @GetMapping("/grantOperatorUser")
    public R<List<Map<String, Object>>> queryOperatorUser() {

        return xhpcCardService.queryOperatorUser();
    }

    /**
     * report the loss of card
     *
     * @author WH
     * @date 2022/2/12 16:58
     * @since version-1.0
     */
    @PatchMapping("/loss")
    public R<Object> reportTheLossOfCard(@RequestParam("cardId") Integer cardId) {

        return xhpcCardService.reportTheLossOfCard(cardId);
    }

    /**
     * query to list of operate card
     *
     * @author WH
     * @date 2022/2/14 10:32
     * @since version-1.0
     */
    @GetMapping("/operateCardsLog")
    public R<OperateCardsLogResponse> operateCardsLog(OperateCardsLogRequest operateCardsLogRequest) {

        return xhpcCardService.operateCardLog(operateCardsLogRequest);
    }

    /**
     * query log of operating devices
     *
     * @author WH
     * @date 2022/2/14 11:13
     * @since version-1.0
     */
    @GetMapping("/operateDevicesLog")
    public R<OperateDevicesLogResponse> operateDevicesLog(@Validated OperateDevicesLogRequest operateDevicesLogRequest) {

        return xhpcCardService.operateDevicesLog(operateDevicesLogRequest);
    }

    /**
     * query recharging records of user
     *
     * @author WH
     * @date 2022/2/14 11:13
     * @since version-1.0
     */
    @GetMapping("/user/recharging")
    public R<QueryRechargingRecordResponse> queryRechargingRecord(QueryRechargingRecordRequest queryRechargingRecordRequest) {

        return xhpcCardService.queryRechargingRecord(queryRechargingRecordRequest);
    }

    /**
     * remove binding of card
     *
     * @author WH
     * @date 2022/2/16 10:10
     * @since version-1.0
     */
    @PatchMapping("/card/removeBinding")
    public R<Object> removeBinding(@RequestParam Long cardRecordId) {

        return xhpcCardService.removeBinding(cardRecordId);

    }

    /**
     * Query client users
     *
     * @author WH
     * @date 2022/2/16 10:10
     * @since version-1.0
     */
    @GetMapping("/card/client/user")
    public R<Object> queryClientUsers(queryClientUsersCondition queryCondition) {

        return xhpcCardService.queryClientUsers(queryCondition);
    }

    /**
     * add a client user
     *
     * @author WH
     * @date 2022/2/24 17:36
     * @since version-1.0
     */
    @PostMapping("/card/client/user")
    public R<Object> addClientUser(@RequestBody TIccardClientUsers userInfo) {

        return xhpcCardService.addClientUser(userInfo);
    }


    /**
     * update client user info
     *
     * @author WH
     * @date 2022/2/24 17:20
     * @since version-1.0
     */
    @PatchMapping("/card/client/user")
    public R<Object> updateClientUserInfo(@RequestBody TIccardClientUsers userInfo) {

        return xhpcCardService.updateClientUser(userInfo);
    }

    /**
     * update client user info
     *
     * @author WH
     * @date 2022/2/24 17:36
     * @since version-1.0
     */
    @DeleteMapping("/card/client/user")
    public R<Object> deleteClientUser(Integer userId) {

        return xhpcCardService.deleteClientUser(userId);
    }

    @PostMapping(value = "/addIccardInfo")
    public R addIccardInfo(@RequestBody TIccardInfo iccardInfo) {
        return xhpcCardService.addIccardInfo(iccardInfo);
    }

    @DeleteMapping("/{id}")
    public R<Object> removeIccardInfo(@PathVariable Integer id) {
        return xhpcCardService.removeIccardInfo(id);
    }



    //定时任务，温江1-5号， 00 停止充电，下发停止充电   06 启动充电
    //@Scheduled(cron = "0 0 0 * * ?")
    public void test1(){

        List<Map<String, Object>> list = xhpcCardService.getSerialNumberList();
        for (int i = 0; i <list.size() ; i++) {
            Map<String, Object> map = list.get(i);
            if(map.get("serialNumber") !=null){
                String serialNumberOrderMap = map.get("serialNumber").toString();
                Map<String, Object> cacheMap = redisService.getCacheMap("gun:" + serialNumberOrderMap.substring(0,16));
                if(cacheMap !=null){
                    if(cacheMap.get("orderkey") !=null){
                        String version ="0C";
                        if(cacheMap.get("version") !=null){
                            version =cacheMap.get("version").toString();
                        }
                        powerPileService.stopCharging(serialNumberOrderMap, serialNumberOrderMap.substring(0,14), serialNumberOrderMap.substring(0,16), version);
                    }
                }
            }
        }
        //停用
        xhpcCardService.updateXhpcTerminalAll(1);

    }

   // @Scheduled(cron = "0 0 6 * * ?")
    public void test2(){

        //停用
        xhpcCardService.updateXhpcTerminalAll(0);

    }


}
