package com.bohuikeji.delivery.backstage.controller.api.admin;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bohuikeji.frame.module.admin.entity.back.APIEntity;
import com.bohuikeji.frame.module.admin.entity.sys.SysConfig;
import com.bohuikeji.frame.module.admin.entity.sys.SysUser;
import com.bohuikeji.frame.module.admin.service.ISysConfigService;
import com.bohuikeji.frame.module.admin.service.ISysUserService;
import com.bohuikeji.frame.module.admin.utils.JwtTokenUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Api(tags = {"管理端APP接口——用户相关"})
@RestController
@RequestMapping("/api/admin/user")
@Slf4j
public class AdminUserApi {

    @Autowired
    ISysUserService sysUserService;
    @Autowired
    ISysConfigService configService;

    @ApiOperation("管理端用户登陆")
    @GetMapping("/login")
    @ApiImplicitParams({
            @ApiImplicitParam(name="account",value="账号",dataType="String", paramType = "query",example="admin",required = true),
            @ApiImplicitParam(name="password",value="密码",dataType="String", paramType = "query",example="admin",required = true)})
    public APIEntity login(String account,String password){
        SysUser user=sysUserService.getOne(
                new QueryWrapper<SysUser>()
                        .eq("user_phone",account)
                        .eq("user_pwd",password)
                        .in("role_id","1","7"));
        if(user==null){
            return APIEntity.fail("登陆失败，账号或密码错误！");
        }
        JSONObject json=new JSONObject();
        json.put("userInfo",user);
        json.put("token",JwtTokenUtil.createToken("管理员",user.getUserId()));
        return APIEntity.success(json);
    }


    @ApiOperation("按ID查询用户信息")
    @GetMapping("/get")
    @ApiImplicitParams({
            @ApiImplicitParam(name="id",value="用户ID",dataType="String", paramType = "query",example="1",required = true)})
    public APIEntity getUser(Integer id){
        SysUser user=sysUserService.getById(id);
        if(user==null){
            return APIEntity.fail("未找到用户信息！");
        }
        return APIEntity.success(user);
    }

    /**
     * 派送员安卓端获取更新
     * @return
     */
    @ApiOperation("管理端APP检测更新——安卓")
    @GetMapping("/android/update")
    @ApiImplicitParams({
            @ApiImplicitParam(name="version",value="版本号",dataType="Integer", paramType = "query",example = "100")})
    public APIEntity courierUpdate(String version){
        SysConfig sys=configService.getOneByKey("admin_apk_version");
        if(!sys.getConfigValue().equals(version)){
            JSONObject json=new JSONObject();
            json.put("url",sys.getConfigDataOne());
            json.put("desc",sys.getConfigDataTwo());
            return APIEntity.success(json);
        }
        return APIEntity.fail("暂无更新！");
    }

}
