package com.xhpc.user.service.impl;

import com.xhpc.common.core.domain.R;
import com.xhpc.common.core.utils.SecurityUtils;
import com.xhpc.common.core.utils.StringUtils;
import com.xhpc.common.core.web.service.BaseService;
import com.xhpc.common.security.service.TokenService;
import com.xhpc.common.util.UserTypeUtil;
import com.xhpc.system.api.domain.SysUser;
import com.xhpc.system.api.model.LoginUser;
import com.xhpc.user.domain.XhpcAppInternetUser;
import com.xhpc.user.mapper.XhpcAppInternetUserMapper;
import com.xhpc.user.service.IXhpcAppInternetUserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;
import java.util.Map;
@Service
public class XhpcAppInternetUserServiceImpl extends BaseService implements IXhpcAppInternetUserService {

    @Resource
    private XhpcAppInternetUserMapper xhpcAppInternetUserMapper;
    @Resource
    private TokenService tokenService;
    @Override
    public R<?> cycLogin(Map<String, Object> map) {
        String deviceCode = StringUtils.valueOf(map.get("deviceCode"));
        String mobile = StringUtils.valueOf(map.get("mobile"));
        String pileCode = StringUtils.valueOf(map.get("pileCode"));
        String thirdCode = StringUtils.valueOf(map.get("thirdCode"));
        String token = StringUtils.valueOf(map.get("token"));
        String sign = StringUtils.valueOf(map.get("sign"));
        System.out.println("============map==============="+map.toString());
        if("".equals(mobile) || mobile ==null){
            return R.fail(500,"请重新扫码进入");
        }
        if("".equals(deviceCode) || deviceCode ==null){
            return R.fail(500,"请重新扫码进入");
        }
        if("".equals(pileCode) || pileCode ==null){
            return R.fail(500,"请重新扫码进入");
        }
        //判断手机号是否存在,不存在添加该账号
        XhpcAppInternetUser appInternetUser = xhpcAppInternetUserMapper.getXhpcAppInternetUserPhone(mobile);
        if(appInternetUser ==null){
            //添加账号
            XhpcAppInternetUser xhpcAppUser = new XhpcAppInternetUser();
            xhpcAppUser.setPhone(mobile);
            String password = mobile.substring(mobile.length() - 6);
            xhpcAppUser.setPassword(SecurityUtils.encryptPassword(password));
            xhpcAppUser.setCreateTime(new Date());
            xhpcAppUser.setTenantId("000000");
            xhpcAppUser.setBalance(new BigDecimal(0));
            xhpcAppInternetUserMapper.insert(xhpcAppUser);
            appInternetUser = xhpcAppInternetUserMapper.getXhpcAppInternetUserPhone(mobile);
        }

        //判断设备编号是否存在正确
        int xhpcTerminal = xhpcAppInternetUserMapper.getXhpcTerminal(pileCode);
        if(xhpcTerminal !=1){
            return R.fail(500,"该充电桩异常，请选择其他充电桩");
        }

        //效验川逸充token



        LoginUser userInfo = new LoginUser();
        SysUser sysUser = new SysUser();
        sysUser.setUserName(appInternetUser.getPhone());
        sysUser.setUserId(appInternetUser.getAppInternetUserId());
        userInfo.setSysUser(sysUser);
        userInfo.setUserType(UserTypeUtil.INTERNET_TYPE);
        userInfo.setUsername(mobile);
        userInfo.setUserid(appInternetUser.getAppInternetUserId());
        userInfo.setTenantId("000000");
        userInfo.setUserTypeUtil(UserTypeUtil.INTERNET);

        Map<String, Object> userToken = tokenService.createToken(userInfo);
        userToken.put("phone",mobile);

        //判断该订单号是否已充电完成、充电中、没有充电订单
        Map<String, Object> xhpcChargeOrder = xhpcAppInternetUserMapper.getXhpcChargeOrder(thirdCode);
        if(xhpcChargeOrder==null){
            userToken.put("orderStatus","-1");
        }else{
            if("-1".equals(xhpcChargeOrder.get("status").toString())){
                userToken.put("orderStatus","-1");
            }else if("0".equals(xhpcChargeOrder.get("status").toString())){
                userToken.put("orderStatus","0");
            }else if("1".equals(xhpcChargeOrder.get("status").toString()) ||"3".equals(xhpcChargeOrder.get("status").toString())){
                userToken.put("orderStatus","1");
            }else if("2".equals(xhpcChargeOrder.get("status").toString())){
                userToken.put("orderStatus","2");
            }{
                userToken.put("orderStatus","-1");
            }
        }
        return R.ok(userToken);
    }
}
