package com.dcsoft.system.ladder.controller;

import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.dcsoft.common.core.constant.Constants;
import com.dcsoft.common.core.web.controller.BaseController;
import com.dcsoft.common.core.web.domain.AjaxResult;
import com.dcsoft.common.core.web.page.TableDataInfo;
import com.dcsoft.system.ladder.domain.EmpowerManageVo;
import com.dcsoft.system.ladder.domain.EmpowerRecordVo;
import com.dcsoft.system.ladder.domain.LadderRecordVo;
import com.dcsoft.system.ladder.domain.TLadderConfig;
import com.dcsoft.system.ladder.service.ITEmpowerManageService;
import com.dcsoft.system.ladder.service.ITLadderConfigService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/empowerManage")
public class TEmpowerManageController extends BaseController {

    @Resource
    private ITEmpowerManageService empowerManageService;

    /**
     * 新增梯控授权管理
     */
    @PostMapping(value = "/saveEmpowerManage")
    public AjaxResult saveEmpowerManage(@RequestBody EmpowerManageVo empowerManageVo) {
        empowerManageService.saveEmpowerManage(empowerManageVo);
        return success();
    }

    /**
     * 查询梯控授权管理列表
     */
    @GetMapping("/list")
    public TableDataInfo list(EmpowerManageVo empowerManageVo) {
        startPage();
        List<EmpowerManageVo> list = empowerManageService.queryEmpowerManagePage(empowerManageVo);
        return getDataTable(list);
    }

    /**
     * 删除梯控授权管理列表
     */
    @PostMapping("/deleteEmpowerManage")
    public AjaxResult deleteEmpowerManage(EmpowerManageVo empowerManageVo) {
        empowerManageService.deleteEmpowerManage(empowerManageVo);
        return success();
    }

    /**
     * 查询梯控授权记录列表
     */
    @PostMapping("/queryEmpowerRecordPage")
    public TableDataInfo queryEmpowerRecordPage(EmpowerManageVo empowerManageVo) {
        startPage();
        List<EmpowerRecordVo> recordVos = empowerManageService.queryEmpowerRecordPage(empowerManageVo);
        return getDataTable(recordVos);
    }

    /**
     * 修改禁用或启用状态
     */
    @PostMapping("/updateLadderState")
    public AjaxResult updateLadderState(EmpowerManageVo empowerManageVo) throws IOException {
        empowerManageService.updateLadderState(empowerManageVo);
        return success();
    }

    /**
     * 删除授权记录
     */
    @PostMapping("/deleteLadderState")
    public AjaxResult deleteLadderState(EmpowerManageVo empowerManageVo) {
        empowerManageVo.setType(Constants.ONE);
        empowerManageService.deleteLadderState(empowerManageVo);
        return success();
    }

    /**
     * 查询识别记录
     */
    @PostMapping("/queryLadderRecord")
    public TableDataInfo queryLadderRecord(LadderRecordVo recordVo) {
        startPage();
        List<LadderRecordVo> recordVos = empowerManageService.queryLadderRecord(recordVo);
        return getDataTable(recordVos);
    }

    /**
     * 删除识别记录
     */
    @PostMapping("/deleteLadderRecord")
    public AjaxResult deleteLadderRecord(LadderRecordVo recordVo) {
        empowerManageService.deleteLadderRecord(recordVo);
        return success();
    }


    @PostMapping("/find")
    public AjaxResult find(LadderRecordVo recordVo) {
        Map<String, String> map = new HashMap<>();
        map.put("pass", recordVo.getPassword());
        map.put("idcardnum", recordVo.getCard());
        String body = HttpUtil.createGet("http://10.1.22.93:8090" + "/cardinfo/find").body(JSON.toJSONString(map)).execute().body();
        return success(body);
    }

    @PostMapping("/SetNetRegister")
    public AjaxResult SetNetRegister(LadderRecordVo recordVo) {
        Map<String, String> map = new HashMap<>();
        map.put("CellPassword", recordVo.getPassword());
        map.put("pass", recordVo.getCard());
        map.put("Oldpass", recordVo.getCard());
        String body = HttpUtil.createPost("http://10.1.22.93:8090" + "/SetNetRegister").body(JSON.toJSONString(map)).execute().body();
        return success(body);
    }

    /**
     * 查询梯控数量（今天，昨天）
     * @param recordVo
     * @return
     */
    @PostMapping("/queryLadderRecordCount")
    public AjaxResult queryLadderRecordCount(LadderRecordVo recordVo) {
        return success(empowerManageService.queryLadderRecordCount(recordVo));
    }





}
