package com.fdkankan.ucenter.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.fdkankan.common.exception.BusinessException;
import com.fdkankan.common.util.SecurityUtil;
import com.fdkankan.sign.AesUtil;
import com.fdkankan.ucenter.constant.LoginConstant;
import com.fdkankan.ucenter.entity.User;
import com.fdkankan.ucenter.service.*;
import com.fdkankan.ucenter.vo.response.UserVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class AppUserService {

    @Autowired
    IUserService userService;
    @Autowired
    ICameraDetailService cameraDetailService;
    @Autowired
    ISceneProService sceneProService;
    @Autowired
    IScenePlusService scenePlusService;
    @Autowired
    IExpansionOrderService expansionOrderService;
    @Autowired
    IUserIncrementService userIncrementService;

    public UserVo getUserInfo(String phoneNum, Integer cameraType) {
        if(StringUtils.isEmpty(phoneNum) ){
            throw new BusinessException(LoginConstant.FAILURE_CODE_3001, LoginConstant.FAILURE_MSG_3001);
        }
        User user = userService.getByUserName(phoneNum);
        if(user == null){
            throw new BusinessException(LoginConstant.FAILURE_CODE_3015, LoginConstant.FAILURE_MSG_3015);
        }
        UserVo userVo = new UserVo();
        BeanUtils.copyProperties(user,userVo);
        Long cameraCount = cameraDetailService.getCountByUserId(user.getId(),cameraType);
        Long proCount = sceneProService.getCountByUserId(user.getId(), cameraType);
        Long plusCount = scenePlusService.getCountByUserId(user.getId(), cameraType);
        Long expansionCount = expansionOrderService.getCount(user.getId());
        Long incrementNum = userIncrementService.getCountByUserId(user.getId(),0);
        Long incrementBindNum = userIncrementService.getCountByUserId(user.getId(),1);

        userVo.setCameraCount(cameraCount);
        userVo.setSceneCount(plusCount + proCount);
        userVo.setExpansionTotal(expansionCount);
        userVo.setIncrementNum(incrementNum);
        userVo.setIncrementBindNum(incrementBindNum);
        try {
            userVo.setPassword(AesUtil.encrypt(SecurityUtil.MD5(userVo.getPassword())));
        } catch (Exception e) {
            userVo.setPassword(userVo.getPassword());
        }
        return userVo;
    }

    public void updateNickName(String nickName, String phoneNum) {
        userService.updateNickName(nickName,phoneNum);
    }

    public void uploadHead(String phoneNum, String head) {
        if (StringUtils.isEmpty(phoneNum) || StringUtils.isEmpty(head)){
            throw new BusinessException(LoginConstant.FAILURE_CODE_3001, LoginConstant.FAILURE_MSG_3001);
        }
        LambdaUpdateWrapper<User> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(User::getUserName,phoneNum)
                .set(User::getHead,head + "?t=" + System.currentTimeMillis());
        userService.update(wrapper);
    }
}
