package com.xhpc.user.service.impl;

import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateUtil;
import com.xhpc.common.core.constant.HttpStatus;
import com.xhpc.common.core.constant.StatusConstants;
import com.xhpc.common.core.constant.UserConstants;
import com.xhpc.common.core.domain.R;
import com.xhpc.common.core.enums.UserStatus;
import com.xhpc.common.core.utils.SecurityUtils;
import com.xhpc.common.core.utils.StringUtils;
import com.xhpc.common.core.web.domain.AjaxResult;
import com.xhpc.common.core.web.service.BaseService;
import com.xhpc.common.redis.service.RedisService;
import com.xhpc.common.security.service.TokenService;
import com.xhpc.common.util.LogUserUtils;
import com.xhpc.common.util.UserTypeUtil;
import com.xhpc.system.api.domain.SysUser;
import com.xhpc.system.api.model.LoginUser;
import com.xhpc.user.controller.XhpcCommonController;
import com.xhpc.user.domain.XhpcAppUser;
import com.xhpc.user.dto.XhpcActivityDiscountDto;
import com.xhpc.user.mapper.XhpcAppUserMapper;
import com.xhpc.user.service.IXhpcAppUserUserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * C端用户信息 服务层
 *
 * @author ruoyi
 */
@Service
public class XhpcAppUserServiceImpl extends BaseService implements IXhpcAppUserUserService {

    @Autowired
    private XhpcAppUserMapper xhpcAppUserMapper;

    @Autowired
    private RedisService redisService;

    @Autowired
    private TokenService tokenService;

    @Autowired
    private LogUserUtils logUserUtils;

    @Autowired
    private XhpcCommonController xhpcCommonController;


    private static final Logger logger = LoggerFactory.getLogger(XhpcAppUserServiceImpl.class);
    /**
     * 更新C端用户
     *
     * @param xhpcAppUser C端用户
     */
    @Override
    @Transactional
    public int update(XhpcAppUser xhpcAppUser) {
        return xhpcAppUserMapper.update(xhpcAppUser);
    }


    /**
     * C端用户详情
     *
     * @param appUserId C端用户id
     * @return 结果
     */
    @Override
    public Map<String, Object> info(Long appUserId) {
        return xhpcAppUserMapper.info(appUserId);
    }

    /**
     * C端用户分页列表
     *
     * @param phone 手机号
     * @return 结果
     */
    @Override
    public List<Map<String, Object>> selectAppUserList(HttpServletRequest request,String phone) {
        LoginUser logUser = logUserUtils.getLogUser(request);
        startPage();
        return xhpcAppUserMapper.selectAppUserList(phone,logUser.getTenantId());
    }

    @Override
    public Map<String, Object> userSum(String phone) {
        LoginUser logUser = tokenService.getLoginUser();

        //昨日用户的活跃度
        Date newDate1 = DateUtil.offset(new Date(), DateField.DAY_OF_MONTH, -1);
        String time1 = DateUtil.format(newDate1, "yyyy-MM-dd HH:mm:ss");
        //7天用户的活跃度
        Date newDate2 = DateUtil.offset(new Date(), DateField.DAY_OF_MONTH, -7);
        String time2 = DateUtil.format(newDate2, "yyyy-MM-dd HH:mm:ss");
        //一个月用户的活跃度
        Date newDate3 = DateUtil.offset(new Date(), DateField.DAY_OF_MONTH, -30);
        String time3 = DateUtil.format(newDate3, "yyyy-MM-dd HH:mm:ss");
        //3个月天用户的活跃度
        Date newDate4 = DateUtil.offset(new Date(), DateField.DAY_OF_MONTH, -90);
        String time4 = DateUtil.format(newDate4, "yyyy-MM-dd HH:mm:ss");

        return xhpcAppUserMapper.userSum(phone,time1,time2,time3,time4,logUser.getTenantId());
    }

    /**
     * 禁用/启用C端用户
     *
     * @param appUserId C端用户id
     * @return 结果
     */
    @Override
    @Transactional
    public void status(Long appUserId) {
        Map<String, Object> map = xhpcAppUserMapper.info(appUserId);
        String status = StringUtils.valueOf(map.get("status"));
        XhpcAppUser xhpcAppUser = new XhpcAppUser();
        xhpcAppUser.setAppUserId(appUserId);
        xhpcAppUser.setStatus(UserConstants.NORMAL == Integer.parseInt(status) ? UserConstants.EXCEPTION : UserConstants.NORMAL);
        update(xhpcAppUser);
    }

    /**
     * C端用户注册
     *
     * @param map C端用户 phone 手机号 code 验证码 type 登录方式1微信2小程序
     * @return 结果
     */
    @Override
    public R<?> register(Map<String, Object> map) {
        String code = StringUtils.valueOf(map.get("code"));
        String phone = StringUtils.valueOf(map.get("phone"));
        String type = StringUtils.valueOf(map.get("type"));
        String openid = StringUtils.valueOf(map.get("openid"));
        String tenantId = StringUtils.valueOf(map.get("tenantId"));
        logger.info("<<<<<<<<<<openid>>>>>>>>>>>>");
        logger.info("<<<<<<<<<<注册openid>>>>>>>>>>>>"+openid);
        logger.info("<<<<<<<<<<openid>>>>>>>>>>>>");
        if (StringUtils.isEmpty(openid)) {
            return R.fail(HttpStatus.NOT_NULL, "openid不能为空");
        }
        if (StringUtils.isEmpty(phone)) {
            return R.fail(HttpStatus.NOT_NULL, "请重新获取手机号");
        }
        if (StringUtils.isEmpty(code)) {
            return R.fail(HttpStatus.NOT_NULL, "验证码不能为空");
        }
        String captcha = redisService.getCacheObject("pvToken:" + phone);
        if(captcha==null || "".equals(captcha)){
           return R.fail(HttpStatus.ERROR_STATUS, "验证码错误");
        }
        if (!code.equalsIgnoreCase(captcha)) {
           return R.fail(HttpStatus.ERROR_STATUS, "验证码错误");
        }
        XhpcAppUser info = xhpcAppUserMapper.getAppUserByPhone(phone,tenantId);
        if (StringUtils.isNotNull(info)) {
            //判断openid是否存在，不存在重新补充
            if (StatusConstants.OPERATION_WX_TYPE.equals(type)) {
                if (StringUtils.isEmpty(info.getWeixinOpenId())) {
                    info.setWeixinOpenId(openid);
                    info.setWeixinLogin(1);
                    xhpcAppUserMapper.update(info);
                }
            } else {
                if (StringUtils.isEmpty(info.getAlipayOpenId())) {
                    info.setAlipayOpenId(openid);
                    info.setAlipayLogin(1);
                    xhpcAppUserMapper.update(info);
                }
            }
            //添加最后一次登录数据
            xhpcAppUserMapper.addUserLoginTime(info.getAppUserId(),info.getPhone(),UserTypeUtil.USER_TYPE,openid,Integer.valueOf(type),null,tenantId,new Date());
            return R.fail(HttpStatus.ALREADY_EXISTING, "账号已存在");
        }
        XhpcAppUser xhpcAppUser = new XhpcAppUser();
        xhpcAppUser.setPhone(phone);
        String password = phone.substring(phone.length() - 6);
        xhpcAppUser.setPassword(SecurityUtils.encryptPassword(password));
        xhpcAppUser.setCreateTime(new Date());
        xhpcAppUser.setTenantId(tenantId);
        xhpcAppUserMapper.insert(xhpcAppUser);
        redisService.deleteObject("pvToken:"+phone);
        return appLogin(phone, type, openid, tenantId);
    }

    /**
     * C端用户登录
     *
     * @param map C端用户 phone 手机号 code 验证码 type 登录方式1微信2小程序
     * @return 结果
     */
    @Override
    public R<?> login(Map<String, Object> map) {
        String code = StringUtils.valueOf(map.get("code"));
        String phone = StringUtils.valueOf(map.get("phone"));
        String type = StringUtils.valueOf(map.get("type"));
        String tenantId = StringUtils.valueOf(map.get("tenantId"));
        logger.info("<<<<<<<<<<code>>>>>>>>>>>>"+code);
        logger.info("<<<<<<<<<<tenantId>>>>>>>>>>>>"+tenantId);
        if("18123374652".equals(phone) && "123456".equals(code)){
            if("1".equals(type)){
                return appLogin(phone, "1", "ot6ul4nlSC5ZZOC4rTLS5hedFTGk", tenantId);
            }
            return appLogin(phone, "2", "2088912814256712", tenantId);
        }
        if (StringUtils.isEmpty(code)) {
            return R.fail(HttpStatus.NOT_NULL, "验证码不能为空");
        }
        String openid = StringUtils.valueOf(map.get("openid"));
        logger.info("<<<<<<<<<<openid>>>>>>>>>>>>");
        logger.info("<<<<<<<<<<登录openid>>>>>>>>>>>>"+openid);

        logger.info("<<<<<<<<<<openid>>>>>>>>>>>>");
        if (StringUtils.isEmpty(openid)) {
            return R.fail(HttpStatus.NOT_NULL, "openid不能为空");
        }
        String captcha = redisService.getCacheObject("pvToken:" + phone);
        if(null ==captcha || "".equals(captcha)){
            return R.fail(HttpStatus.ERROR_STATUS, "验证码错误!");
        }
        if (!code.equalsIgnoreCase(captcha)) {
            return R.fail(HttpStatus.ERROR_STATUS, "验证码错误");
        }
        return appLogin(phone, type, openid, tenantId);
    }

    /**
     * 登录
     */
    @Transactional
    public R<?> appLogin(String username, String type, String openid, String tenantId) {
        //统一沉睡300毫秒防止重复提交
//        try {
//            Thread.sleep(300);
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        }
        // 用户名或密码为空 错误
        if (StringUtils.isAnyBlank(username)) {
            return R.fail(HttpStatus.NOT_NULL, "用户必须填写");
        }
        // 用户名不在指定范围内 错误
        if (username.length() < UserConstants.USERNAME_MIN_LENGTH
                || username.length() > UserConstants.USERNAME_MAX_LENGTH) {
            return R.fail(HttpStatus.ERROR_STATUS, "用户名不在指定范围");
        }
        String sub = username.substring(0, 2);
        if (UserTypeUtil.COMMUNIT.equals(sub) || UserTypeUtil.CUSTOMERS.equals(sub)){
            R lognUser = xhpcCommonController.getMechanism(username, null, null, null, tenantId);
            if(lognUser !=null && lognUser.getData() !=null){
                Map<String, Object> map = (Map<String, Object>)lognUser.getData();
                LoginUser userInfo = new LoginUser();
                SysUser sysUser = new SysUser();
                sysUser.setUserName(username);
                long appUserId = Long.parseLong(map.get("appUserId").toString());
                sysUser.setUserId(appUserId);
                if(map.get("tenantStatus") !=null && "2".equals(map.get("tenantStatus").toString())){
                    userInfo.setStatus(2);
                }else{
                    userInfo.setStatus(1);
                }
                userInfo.setSysUser(sysUser);
                userInfo.setUsername(username);
                userInfo.setOpenId(openid);
                userInfo.setTenantId(map.get("tenantId").toString());
                if(UserTypeUtil.COMMUNIT.equals(username.substring(0,2))){
                    userInfo.setUserType(UserTypeUtil.COMMUNIT_TYPE);
                    userInfo.setUserTypeUtil(UserTypeUtil.COMMUNIT);
                }else{
                    userInfo.setUserType(UserTypeUtil.CUSTOMERS_TYPE);
                    userInfo.setUserTypeUtil(UserTypeUtil.CUSTOMERS);
                }
                if(UserTypeUtil.COMMUNIT.equals(sub)){
                    if (StatusConstants.OPERATION_WX_TYPE.equals(type)) {
                        xhpcAppUserMapper.updateCommunityPersonnel(appUserId,openid,null,UserConstants.LOGIN,null);
                    } else {
                        xhpcAppUserMapper.updateCommunityPersonnel(appUserId,null,openid,null,UserConstants.LOGIN);
                    }
                }else{
                    if (StatusConstants.OPERATION_WX_TYPE.equals(type)) {
                        xhpcAppUserMapper.updateCustomersPersonnel(appUserId,openid,null,UserConstants.LOGIN,null);
                    } else {
                        xhpcAppUserMapper.updateCustomersPersonnel(appUserId,null,openid,null,UserConstants.LOGIN);
                    }
                }
                //去退出账号之前登录的信息
                xhpcAppUserMapper.updateUserLoginTime(username);
                //添加最后一次登录数据
                xhpcAppUserMapper.addUserLoginTime(appUserId,username,userInfo.getUserType(),openid,Integer.valueOf(type),null,tenantId,new Date());

                redisService.deleteObject("pvToken:" + username);
                // 获取登录token

                Map<String, Object> token = tokenService.createToken(userInfo);
                return R.ok(token);
            }else{
                return R.fail(HttpStatus.DATA_ERROR, "无此账号，请重新输入账号登录");
            }
        }
            //C端用户自动注册
            XhpcAppUser user = xhpcAppUserMapper.getAppUserByPhone(username,tenantId);
            if (StringUtils.isNull(user)) {
                //注册
                XhpcAppUser xhpcAppUser = new XhpcAppUser();
                xhpcAppUser.setPhone(username);
                String password = username.substring(username.length() - 6);
                xhpcAppUser.setPassword(SecurityUtils.encryptPassword(password));
                xhpcAppUser.setCreateTime(new Date());
                if (StatusConstants.OPERATION_WX_TYPE.equals(type)) {
                    xhpcAppUser.setWeixinOpenId(openid);
                    xhpcAppUser.setWeixinLogin(1);
                } else {
                    xhpcAppUser.setAlipayOpenId(openid);
                    xhpcAppUser.setAlipayLogin(1);
                }
                xhpcAppUser.setTenantId(tenantId);
                xhpcAppUserMapper.insert(xhpcAppUser);
                user = xhpcAppUserMapper.getAppUserByPhone(username,tenantId);
                // 获取登录token
                //return R.ok(tokenService.createToken(userInfo));
                //return R.fail(HttpStatus.DATA_ERROR, "登录用户：" + username + " 不存在");
            }
            LoginUser userInfo = new LoginUser();
            SysUser sysUser = new SysUser();
            sysUser.setUserName(user.getPhone());
            sysUser.setUserId(user.getAppUserId());
            userInfo.setSysUser(sysUser);
            userInfo.setUserType(UserTypeUtil.USER_TYPE);
            userInfo.setUsername(username);
            userInfo.setOpenId(openid);
            userInfo.setUserid(user.getAppUserId());
            userInfo.setTenantId(tenantId);
            userInfo.setUserTypeUtil(UserTypeUtil.USER);
            if (UserStatus.DELETED.getCode().equals(user.getDelFlag())) {
                return R.fail(HttpStatus.DATA_ERROR, "对不起，您的账号：" + username + " 已被删除");
            }
            if (UserStatus.DISABLE.getCode().equals(user.getStatus())) {
                return R.fail(HttpStatus.DATA_ERROR, "对不起，您的账号：" + username + " 已停用");
            }
            if (StatusConstants.OPERATION_WX_TYPE.equals(type)) {
                user.setWeixinOpenId(openid);
                user.setWeixinLogin(UserConstants.LOGIN);
            } else {
                user.setAlipayOpenId(openid);
                user.setAlipayLogin(UserConstants.LOGIN);
            }
            xhpcAppUserMapper.update(user);
            redisService.deleteObject("pvToken:" + user.getPhone());
            //去退出账号之前登录的信息
            xhpcAppUserMapper.updateUserLoginTime(username);
            //添加最后一次登录数据
            xhpcAppUserMapper.addUserLoginTime(user.getAppUserId(),username,userInfo.getUserType(),openid,Integer.valueOf(type),UserConstants.LOGIN,tenantId,new Date());
            // 获取登录token
            Map<String, Object> token = tokenService.createToken(userInfo);
             return R.ok(token);
    }

    /**
     * 小程序用户退出
     *
     * @param request
     * @return 结果
     */
    @Override
    @Transactional
    public R<?> loginOut(HttpServletRequest request, String type) {
        LoginUser loginUser = tokenService.getLoginUser(request);
        if (StringUtils.isNotNull(loginUser)) {
            String username = loginUser.getUsername();
            Integer userType = loginUser.getUserType();
            Long userid = loginUser.getUserid();
            String openId = loginUser.getOpenId();
            String tenantId = loginUser.getTenantId();
            R user = xhpcCommonController.getMechanism(null, userid, userType, null, tenantId);
            if(user !=null && user.getData() !=null){
                if(UserTypeUtil.USER_TYPE==userType){
                    XhpcAppUser appUser = xhpcAppUserMapper.getAppUserByPhone(username,tenantId);
                    if (!StringUtils.isNull(appUser)) {
                        if (StatusConstants.OPERATION_WX_TYPE.equals(type)) {
                            appUser.setWeixinLogin(UserConstants.NO_LOGIN);
                        } else {
                            appUser.setAlipayLogin(UserConstants.NO_LOGIN);
                        }
                        xhpcAppUserMapper.update(appUser);
                        //添加最后一次登录数据
                        xhpcAppUserMapper.addUserLoginTime(appUser.getAppUserId(),username,userType,openId,Integer.valueOf(type),UserConstants.NO_LOGIN,tenantId,new Date());
                    }
                }else if(UserTypeUtil.COMMUNIT_TYPE.equals(userType)){
                    if (StatusConstants.OPERATION_WX_TYPE.equals(type)) {
                        xhpcAppUserMapper.updateCommunityPersonnel(userid,null,null,UserConstants.NO_LOGIN,null);
                    } else {
                        xhpcAppUserMapper.updateCommunityPersonnel(userid,null,null,null,UserConstants.NO_LOGIN);
                    }
                    //添加最后一次登录数据
                    xhpcAppUserMapper.addUserLoginTime(userid,username,userType,openId,Integer.valueOf(type),UserConstants.NO_LOGIN,tenantId,new Date());
                }else if(UserTypeUtil.CUSTOMERS_TYPE.equals(userType)){
                    if (StatusConstants.OPERATION_WX_TYPE.equals(type)) {
                        xhpcAppUserMapper.updateCustomersPersonnel(userid,null,null,UserConstants.NO_LOGIN,null);
                    } else {
                        xhpcAppUserMapper.updateCustomersPersonnel(userid,null,null,null,UserConstants.NO_LOGIN);
                    }
                    //添加最后一次登录数据
                    xhpcAppUserMapper.addUserLoginTime(userid,username,userType,openId,Integer.valueOf(type),UserConstants.NO_LOGIN,tenantId,new Date());
                }
            }
            // 删除用户缓存记录
            tokenService.delLoginUser(loginUser.getToken());
        }
        return R.ok();
    }

    /**
     * C端用户自动登录
     *
     * @param map type 登录方式1微信2小程序 openid 微信或支付宝openid
     * @return
     */
    @Override
    public R<?> voluntaryLogin(Map<String, Object> map) {
        String type = StringUtils.valueOf(map.get("type"));
        String openid = StringUtils.valueOf(map.get("openid"));
        String tenantId = StringUtils.valueOf(map.get("tenantId"));
        logger.info("<<<<<<<<<<openid>>>>>>>>>>>>");
        logger.info("<<<<<<<<<<自动登录openid>>>>>>>>>>>>"+openid);
        logger.info("<<<<<<<<<<openid>>>>>>>>>>>>");
        Map<String, Object> userLoginTime = xhpcAppUserMapper.getUserLoginTime(Integer.valueOf(type), openid,tenantId);
        if(userLoginTime ==null){
            return R.fail(HttpStatus.USER_LOGIN, "请重新登录");
        }
        logger.info("-------userLoginTime---" + userLoginTime);
        if(userLoginTime.get("status") == null){
            return R.fail(HttpStatus.USER_LOGIN, "请重新登录");
        } else if(UserTypeUtil.NO_LOGIN.equals(userLoginTime.get("status").toString())){
            return R.fail(HttpStatus.USER_LOGIN, "请重新登录");
        }
        logger.info("-------status---"+userLoginTime.get("status").toString());
        return appLogin(userLoginTime.get("account").toString(), type, openid, tenantId);
    }

    /**
     * 小程序用户详情
     * @param
     * @return 结果
     */
    @Override
    public AjaxResult appInfo(HttpServletRequest request) {
        try{
            LoginUser loginUser = tokenService.getLoginUser();
            String tenantId = loginUser.getTenantId();
            Object version = redisService.getCacheObject("global:"+tenantId+":version");
            Object servicePhone = redisService.getCacheObject("global:"+tenantId+":phone");
            //根据不同的用户类型查询不同的信息
            Long userid = loginUser.getUserid();
            Integer userType = loginUser.getUserType();
            R user = xhpcCommonController.getMechanism(null, userid, userType, null, tenantId);
            if(user !=null && user.getData() !=null){
                Map<String, Object> map = (Map<String, Object>)user.getData();
                //发票、留言板、版本、客服电话
//                Long invoice =null;
//                String globalInvoice="global:invoice:"+ tenantId +":";
//                if(UserTypeUtil.USER_TYPE.equals(userType)){
//                    invoice = redisService.getCacheObject(globalInvoice + UserTypeUtil.USER + userid);
//                }else if(UserTypeUtil.INTERNET_TYPE.equals(userType)){
//                    invoice = redisService.getCacheObject(globalInvoice+UserTypeUtil.INTERNET+userid);
//                }else if(UserTypeUtil.COMMUNIT_TYPE.equals(userType)){
//                    invoice = redisService.getCacheObject(globalInvoice+UserTypeUtil.COMMUNIT+userid);
//                }else{
//                    invoice = redisService.getCacheObject(globalInvoice+UserTypeUtil.CUSTOMERS+userid);
//                }
//                if(invoice==null){
//                    map.put("invoiceNumber","0");
//                }else{
//                    map.put("invoiceNumber",invoice);
//                }
                map.put("invoiceNumber","0");
//                Integer guestbook =null;
//                if(UserTypeUtil.USER_TYPE.equals(userType)){
//                    guestbook = redisService.getCacheObject("userNotReadMessageCount:"+UserTypeUtil.USER_TYPE + ":" + map.get("phone").toString() + ":0:" + loginUser.getTenantId());
//                }else if(UserTypeUtil.INTERNET_TYPE.equals(userType)){
//                    guestbook = redisService.getCacheObject("userNotReadMessageCount:"+UserTypeUtil.INTERNET_TYPE + ":" + map.get("phone").toString() + ":0:" + loginUser.getTenantId());
//                }else if(UserTypeUtil.COMMUNIT_TYPE.equals(userType)){
//                    guestbook = redisService.getCacheObject("userNotReadMessageCount:"+UserTypeUtil.COMMUNIT_TYPE + ":" + map.get("account").toString() + ":0:" + loginUser.getTenantId());
//                }else{
//                    guestbook = redisService.getCacheObject("userNotReadMessageCount:"+UserTypeUtil.CUSTOMERS_TYPE + ":" + map.get("account").toString() + ":0:" + loginUser.getTenantId());
//                }
//                if(guestbook==null){
//                    map.put("guestbook","0");
//                }else{
//                    map.put("guestbook",guestbook);
//                }
                map.put("guestbook","0");
                map.put("version",version);
                map.put("servicePhone",servicePhone);
                //获取最优的活动图片显示
                Map<String, Object> objectMap = activityDiscountTime(loginUser.getUserid(), new Date(), loginUser.getUserType(), null, loginUser.getTenantId());
//                if("1".equals(objectMap.get("state").toString())){
//                    map.put("activity",1);
//                    map.put("activitySize",1);
//                    map.put("activityImg","https://xhpc-bucket1.oss-cn-hangzhou.aliyuncs.com/"+objectMap.get("totalBackgroundUrl").toString());
//                }else{
//                    map.put("activity",0);
//                    map.put("activitySize",1);
//                    map.put("activityImg","https://xhpc-bucket1.oss-cn-hangzhou.aliyuncs.com/bunengshanchu/1.png");
//                }
                map.put("activity",0);
                map.put("activitySize",1);
                map.put("activityImg","https://xhpc-bucket1.oss-cn-hangzhou.aliyuncs.com/bunengshanchu/1.png");
                map.put("socSize",redisService.getCacheObject("global:"+tenantId+":SOC"));
                return AjaxResult.success(map);
            }else{
                return AjaxResult.error("请重新登录",HttpStatus.USER_LOGIN);
            }
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.error("请重新登录",HttpStatus.USER_LOGIN);
        }
    }

    /**
     * 注销账号
     *
     * @param phone
     * @param code
     * @return
     */
    @Override
    @Transactional
    public R<?> logout(HttpServletRequest request,String phone, String code) {

        if (StringUtils.isEmpty(code)) {
            return R.fail(HttpStatus.NOT_NULL, "验证码不能为空");
        }
        if (StringUtils.isEmpty(phone)) {
            return R.fail(HttpStatus.NOT_NULL, "手机号不能为空");
        }
        String captcha = redisService.getCacheObject("pvToken:" + phone);
        if(null ==captcha || "".equals(captcha)){
            return R.fail(HttpStatus.ERROR_STATUS, "验证码错误");
        }
        if (!code.equalsIgnoreCase(captcha)) {
            return R.fail(HttpStatus.ERROR_STATUS, "验证码错误");
        }
        LoginUser loginUser = tokenService.getLoginUser(request);
        Long userid = loginUser.getUserid();
        Integer userType = loginUser.getUserType();
        String tenantId = loginUser.getTenantId();
        if(UserTypeUtil.USER_TYPE.equals(userType)){
            XhpcAppUser xhpcAppUser = xhpcAppUserMapper.getAppUserByPhone(phone,tenantId);
            if (StringUtils.isNull(xhpcAppUser)) {
                return R.fail(HttpStatus.ERROR_STATUS, "用户不存在");
            }
            xhpcAppUserMapper.deleteById(xhpcAppUser.getAppUserId());
        }else{
            return R.fail(HttpStatus.ERROR_STATUS, "用户不能注销");
        }
        redisService.deleteObject("pvToken:" + phone);
        return R.ok();
    }

    @Override
    public R<?> updateIsRefund(Long userId, Integer userType, Integer isRefund) {

        if(isRefund>2 || isRefund<0){
            return R.fail(HttpStatus.ERROR_STATUS, "参数错误");
        }
        if(userType==0){
            int i = xhpcAppUserMapper.updateUserIsRefund(userId, isRefund);
            if(i>0){
                return R.ok();
            }else{
                return R.fail(HttpStatus.ERROR_STATUS, "修改失败");
            }
        }else if(userType==2){
            int i = xhpcAppUserMapper.updateCommunityIsRefund(userId, isRefund,null,null);
            if(i>0){
                return R.ok();
            }else{
                return R.fail(HttpStatus.ERROR_STATUS, "修改失败");
            }
        }
        return R.fail(HttpStatus.ERROR_STATUS, "参数错误");
    }

    @Override
    public R<?> batteryProtect(HttpServletRequest request, Integer soc,Integer socProtect,Integer userType) {
        LoginUser loginUser = logUserUtils.getLogUser(request);
        //根据不同的用户类型查询不同的信息
        Long userid = loginUser.getUserid();
        R user = xhpcCommonController.getMechanism(null, loginUser.getUserid(), loginUser.getUserType(), null,
                loginUser.getTenantId());
        if(user !=null && user.getData() !=null){
            Map<String, Object> map = (Map<String, Object>)user.getData();
            if(UserTypeUtil.USER_TYPE.equals(userType)){
                XhpcAppUser appUser = xhpcAppUserMapper.getAppUserByPhone(map.get("phone").toString(),loginUser.getTenantId());
                appUser.setSocProtect(socProtect);
                appUser.setSoc(soc);
                xhpcAppUserMapper.update(appUser);
            }else if(UserTypeUtil.COMMUNIT_TYPE.equals(userType)){
                xhpcAppUserMapper.updateCommunityIsRefund(userid, null,socProtect,soc);
            }else if(UserTypeUtil.CUSTOMERS_TYPE.equals(userType)){
                xhpcAppUserMapper.updateCustomersSocProtect(userid,socProtect,soc);
            }
            return R.fail(HttpStatus.SUCCESS, "设置成功");
        }
        return R.fail(HttpStatus.ERROR_STATUS, "设置失败");
    }

    //获取用户的活动
    public Map<String,Object> activityDiscountTime(Long userId,Date startTime,Integer source,Long chargingStationId,String tenantId){

        Map<String,Object> map =new HashMap<>();
        List<XhpcActivityDiscountDto> activityDiscountTime = xhpcAppUserMapper.getActivityDiscountTime(userId, DateUtil.formatDateTime(startTime), source, chargingStationId, tenantId);
        if(activityDiscountTime !=null && activityDiscountTime.size()>0){
            //折扣电费、服务费
            BigDecimal totalDiscountRate =new BigDecimal(0);
            int totalDiscountType = 0;
            String totalBackgroundUrl="";
            for (int i = 0; i < activityDiscountTime.size(); i++) {
                XhpcActivityDiscountDto activity = activityDiscountTime.get(i);
                BigDecimal discountRate = activity.getDiscountRate();
                Integer discountType = activity.getDiscountType();
                String backgroundUrl = activity.getBackgroundUrl();
                if(discountType.equals(1)){
                    if(totalDiscountType==0){
                        totalDiscountRate =discountRate;
                        totalDiscountType = 1;
                        totalBackgroundUrl =backgroundUrl;
                    }else if(totalDiscountType==1 || totalDiscountType==3){
                        if(totalDiscountRate.compareTo(discountRate)<0){
                            totalDiscountRate =discountRate;
                            totalDiscountType=1;
                            totalBackgroundUrl =backgroundUrl;
                        }
                    }else{
                        if(totalDiscountRate.compareTo(discountRate.multiply(new BigDecimal(2)))<0){
                            totalDiscountRate =discountRate;
                            totalDiscountType = 1;
                            totalBackgroundUrl =backgroundUrl;
                        }
                    }
                }else if(discountType.equals(2)){
                    if(totalDiscountType==0){
                        totalDiscountRate =discountRate;
                        totalDiscountType = 2;
                        totalBackgroundUrl =backgroundUrl;
                    }else if(totalDiscountType==1 || totalDiscountType==3){
                        if(totalDiscountRate.multiply(new BigDecimal(2)).compareTo(discountRate)<0){
                            totalDiscountRate =discountRate;
                            totalDiscountType = 2;
                            totalBackgroundUrl =backgroundUrl;
                        }
                    }else {
                        if(totalDiscountRate.compareTo(discountRate)<0){
                            totalDiscountRate =discountRate;
                            totalBackgroundUrl =backgroundUrl;
                        }
                    }
                }else{
                    //电量抽成
                    if(totalDiscountType==0){
                        totalDiscountRate =discountRate;
                        totalDiscountType = 3;
                        totalBackgroundUrl =backgroundUrl;
                    }else if(totalDiscountType==1 || totalDiscountType==3){
                        if(totalDiscountRate.compareTo(discountRate)<0){
                            totalDiscountRate =discountRate;
                            totalDiscountType = 3;
                            totalBackgroundUrl =backgroundUrl;
                        }
                    }else{
                        if(totalDiscountRate.multiply(new BigDecimal(2)).compareTo(discountRate)<0){
                            totalDiscountRate =discountRate;
                            totalDiscountType = 3;
                            totalBackgroundUrl =backgroundUrl;
                        }
                    }
                }
            }
            map.put("state","1");
            map.put("totalDiscountRate",totalDiscountRate);
            map.put("totalDiscountType",totalDiscountType+"");
            map.put("totalBackgroundUrl",totalBackgroundUrl);
        }else{
            map.put("state","0");
        }
        return map;
    }



}
