package com.sqx.modules.app.controller.app;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sqx.common.utils.PageUtils;
import com.sqx.common.utils.Result;
import com.sqx.modules.app.annotation.Login;
import com.sqx.modules.app.annotation.LoginUser;
import com.sqx.modules.app.entity.UserEntity;
import com.sqx.modules.app.entity.queryRidersVo;
import com.sqx.modules.app.service.AppService;
import com.sqx.modules.app.service.UserService;
import com.sqx.modules.common.service.CommonInfoService;
import com.sqx.modules.errand.vo.queryOrderInfoVo;
import com.sqx.modules.utils.MD5Util;
import icu.xuyijie.secureapi.annotation.DecryptParam;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * APP登录授权
 */
@RestController
@RequestMapping("/app/user")
@Api(value = "APP管理", tags = {"APP管理"})
public class AppController {

    @Autowired
    private UserService userService;
    @Autowired
    private AppService appService;
    @Autowired
    private CommonInfoService commonInfoService;

    @Login
    @GetMapping(value = "findRiders")
    @ApiOperation("查找被转单骑手")
    public Result findRiders(@RequestAttribute Long userId, Long page, Long limit, String keyword) {

        UserEntity userEntity = userService.selectUserById(userId);
        if (userEntity != null) {
            //tb_user表中的根据rider_open_id是不是null判断他是不是骑手 标识当前用户是骑手，
            //tb_user表中的rider_station_id 标识当前骑手所在站点，
            //tb_user表中的on_line_flag 中存储的值0是休息，1是接单中；
            //查找被转单骑手是要找同一个站点，并且接单中的
            Integer ursIds = userEntity.getRiderStationId();
            if(ursIds == null){
                IPage<queryRidersVo> pages = new Page<>();
                PageUtils pageUtils = new PageUtils(pages);
                return Result.success().put("data", pageUtils);
            }
            Result result = userService.queryRiders(page, limit,userId, ursIds,keyword);
            return result;
        }
        return Result.error("用户不存在！");
    }

    @Login
    @RequestMapping(value = "/updatePwd", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation("用户端修改密码")
    public Result updatePwd(@LoginUser UserEntity user, String pwd, String oldPwd) {
        if (!user.getPassword().equals(MD5Util.md5Encrypt32Upper(oldPwd))) {
            return Result.error("原始密码不正确！");
        }
        if (pwd.equals(oldPwd)) {
            return Result.error("新密码不能与旧密码相同！");
        }
        user.setPassword(MD5Util.md5Encrypt32Upper(pwd));
        userService.updateById(user);
        return Result.success();
    }

    @Login
    @RequestMapping(value = "/updatePhone", method = RequestMethod.POST)
    @ApiOperation("用户端换绑手机号")
    @ResponseBody
    public Result updatePhone(@RequestAttribute("userId") Long userId, @DecryptParam String phone, @DecryptParam String msg) {
        return userService.updatePhone(phone, msg, userId);
    }

    @Login
    @RequestMapping(value = "/updateUser", method = RequestMethod.POST)
    @ApiOperation("用户修改个人信息")
    @ResponseBody
    public Result updateUserImageUrl(@RequestAttribute("userId") Long userId, @RequestBody UserEntity userEntity) {
        userEntity.setUserId(userId);
        userService.updateById(userEntity);
        return Result.success();
    }


    @Login
    @RequestMapping(value = "/updateUserImageUrl", method = RequestMethod.POST)
    @ApiOperation("用户修改头像")
    @ResponseBody
    public Result updateUserImageUrl(@LoginUser UserEntity user, String avatar) {
        user.setAvatar(avatar);
        userService.updateById(user);
        return Result.success();
    }

    @Login
    @RequestMapping(value = "/updateUserName", method = RequestMethod.POST)
    @ApiOperation("用户修改昵称")
    @ResponseBody
    public Result updateUserName(@LoginUser UserEntity user, String userName) {
        user.setUserName(userName);
        userService.updateById(user);
        return Result.success();
    }

    @Login
    @RequestMapping(value = "/selectUserById", method = RequestMethod.GET)
    @ApiOperation("获取用户详细信息")
    @ResponseBody
    public Result selectUserById(@RequestAttribute("userId") Long userId, @LoginUser UserEntity user) {
        return Result.success().put("data", user);
    }


    @RequestMapping(value = "/selectNewApp", method = RequestMethod.GET)
    @ApiOperation("升级检测")
    @ResponseBody
    public Result selectNewApp() {
        return Result.success().put("data", appService.selectNewApp());
    }

    @Login
    @RequestMapping(value = "/updateClientId", method = RequestMethod.POST)
    @ApiOperation("用户端绑定消息推送clientId")
    @ResponseBody
    public Result updateClientId(@DecryptParam String clientId, @RequestAttribute Long userId, Integer sysPhone) {
        UserEntity userEntity = userService.getById(userId);
        if (userEntity != null) {
            //检查是否有其他用户设置这个clientId 如果有 则清除其他用户
            userService.updateUserClientIdIsNull(clientId);
            userEntity.setClientid(clientId);
            userEntity.setSysPhone(sysPhone);
            userService.updateById(userEntity);
            return Result.success();
        }
        return Result.error("用户不存在！");
    }


    @Login
    @RequestMapping(value = "/updateRiderClientId", method = RequestMethod.POST)
    @ApiOperation("骑手端绑定消息推送clientId")
    @ResponseBody
    public Result updateRiderClientId(@DecryptParam String clientId, @RequestAttribute Long userId, Integer sysPhone) {
        UserEntity userEntity = userService.selectUserById(userId);
        if (userEntity != null) {
            userService.updateUserRiderClientIdIsNull(clientId);
            userEntity.setRiderClientid(clientId);
            userEntity.setSysPhone(sysPhone);
            userService.updateById(userEntity);
            return Result.success();
        }
        return Result.error("用户不存在！");
    }


}
