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

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bohuikeji.delivery.backstage.constants.WxAppEm;
import com.bohuikeji.delivery.backstage.entity.TCourier;
import com.bohuikeji.delivery.backstage.entity.TOrder;
import com.bohuikeji.delivery.backstage.service.ITCourierService;
import com.bohuikeji.delivery.backstage.service.ITNewsService;
import com.bohuikeji.delivery.backstage.util.SanYanUtils;
import com.bohuikeji.delivery.backstage.util.WxUtils;
import com.bohuikeji.frame.module.admin.base.ApiController;
import com.bohuikeji.frame.module.admin.entity.back.APIEntity;
import com.bohuikeji.frame.module.admin.utils.JwtTokenUtil;
import com.bohuikeji.frame.module.admin.utils.NumberUtils;
import com.bohuikeji.frame.module.admin.utils.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
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.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author Admin
 */
@Slf4j
@Api(tags = {"v2版本登录授权"})
@RestController
@RequestMapping("/api/v2/login")
@RequiredArgsConstructor
public class LoginV2Api extends ApiController {

    @Autowired
    ITCourierService courierService;
    @Autowired
    ITNewsService newsService;

    @ApiOperation(value = "一键登录",notes = "使用方法详见微信文档：http://shanyan.253.com/document/details?lid=300&cid=93&pc=28&pn=%25E9%2597%25AA%25E9%25AA%258CSDK")
    @GetMapping("/get/query")
    public APIEntity getClientLink(@ApiParam(name = "token",value = "") @RequestParam String token){
        String phone;
        try {
            phone= SanYanUtils.getPhone(token);
            if(StringUtils.isBlank(phone)){
                return APIEntity.fail("您好，当前手机号暂不持支一键登录，请使用验证码登录。");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return APIEntity.fail("授权异常！");
        }
        TCourier courier= courierService.getOne(new QueryWrapper<TCourier>().notIn("customer_status","已注销").eq("courier_phone",phone));
        if(courier != null) {
            if("禁止登陆".equals(courier.getCourierStatus())){
                return APIEntity.fail("您的账号已被禁止登陆，请联系客服或者平台管理员！");
            }
            return APIEntity.success(JwtTokenUtil.createToken("派送员",courier.getCourierId()));
        }else {
            courier = new TCourier();
            String num= NumberUtils.getShort();
            courier.setCourierName("派送员"+num);
            courier.setCourierNumber(num);
            courier.setCourierPhone(phone);
            courier.setCourierPwd(NumberUtils.getPwd());
            //courier.setCid(cid);
            if (!courierService.save(courier)) {
                log.error("派送员注册失败，创建失败，派送员：" + courier);
                return APIEntity.fail("系统繁忙，请稍后重试！");
            }
            newsService.pushCourierMsg1(courier.getCourierId(),"注册成功提醒","您好，您已成功注册成为”同城急送“派送员，默认登陆密码：{0}请及时修改。",courier.getCourierPwd());
            log.info("当前注册派送员[{}],ID=[{}]",courier.getCourierPhone(),courier.getCourierId());
            return APIEntity.success(JwtTokenUtil.createToken("派送员",courier.getCourierId()));
        }
    }
}
