package com.tofly.third.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.tofly.common.core.util.StringUtil;
import com.tofly.third.common.req.FkztReq;
import com.tofly.third.common.res.FkztRes;
import com.tofly.third.common.res.WzxZlRes;
import com.tofly.third.common.res.ZnbRes;
import com.tofly.third.common.req.ZnbZlReq;
import com.tofly.third.common.req.Cbxx;
import com.tofly.third.common.res.ResultRespone;
import com.tofly.third.common.util.TokenUtil;
import com.tofly.third.entity.ApiUser;
import com.tofly.third.service.ApiUserService;
import com.tofly.third.service.YccbService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Slf4j
@RestController
@Api("远传抄表API管理")
@RequestMapping("/yccb")
public class YccbController {

    @Autowired
    private YccbService yccbService;
    @Autowired
    private ApiUserService apiUserService;

    @PostMapping("/cbtb")
    @ApiOperation(value = "物联网表抄表同步(单条)")
    public ResultRespone<ZnbRes> cbtb(@RequestBody @Valid Cbxx cbxx) {
        log.info("物联网表抄表同步(单条)" + JSONObject.toJSONString(cbxx));
        return ResultRespone.success(yccbService.cbtb(cbxx));
    }

    @PostMapping("/fkzlhd")
    @ApiOperation(value = "阀控指令回调")
    public ResultRespone<Void> fkzlhd(@RequestBody @Valid ZnbZlReq znbZlReq) {
        log.info("阀控指令回调" + JSONObject.toJSONString(znbZlReq));
        yccbService.fkzlhd(znbZlReq);
        return ResultRespone.success();
    }

    @PostMapping("/fkzt")
    @ApiOperation(value = "根据命令ID水表编号查询阀控状态")
    public ResultRespone<FkztRes> getFkzt(@RequestBody @Valid FkztReq fkztReq) {
        return ResultRespone.success(yccbService.getFkzt(fkztReq));
    }

    @GetMapping("/wzxml")
    @ApiOperation(value = "获取未执行阀控命令")
    public ResultRespone<WzxZlRes> wzxml() {
        return ResultRespone.success(yccbService.wzxml());
    }


    @GetMapping("/login")
    @ApiOperation(value = "验证权限")
    public ResultRespone getLogin(@RequestParam("loginName") String loginName, @RequestParam("loginPass") String loginPass) {

        if (StringUtils.isEmpty(loginName)) {
            log.info("账号为空.");
            return ResultRespone.failed();
        }

        if (StringUtils.isEmpty(loginPass)) {
            log.info("密码为空.");
            return ResultRespone.failed();
        }
        ResultRespone<Object> rest = new ResultRespone<>();
        ApiUser apiUser = apiUserService.isValidateLogin(loginName, loginPass);

        if (StringUtil.isNotNull(apiUser)) {
            Map<String, String> info = new HashMap<>();
            info.put("loginName", loginName);
            info.put("token", TokenUtil.generateToken(loginName, "admin"));
            apiUser.setLastposttime(new Date());
            /*跟新服务时间*/
            UpdateWrapper updateWrapper = new UpdateWrapper();
            updateWrapper.set("lastposttime", new Date());
            updateWrapper.eq("id", apiUser.getId());

            apiUserService.update(null, updateWrapper);

            return ResultRespone.success(info);
        }
        return ResultRespone.failed("登录失败");
    }


    @GetMapping("/test")
    @ApiOperation(value = "Test")
    public ResultRespone getTest() {
        return ResultRespone.success("成功");
    }
}
