package com.sqx.modules.app.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sqx.common.utils.DateUtils;
import com.sqx.common.utils.Result;
import com.sqx.modules.app.dao.VipDetailsDao;
import com.sqx.modules.app.entity.UserEntity;
import com.sqx.modules.app.entity.VipDetails;
import com.sqx.modules.app.service.UserService;
import com.sqx.modules.app.service.VipDetailsService;
import com.sqx.modules.message.entity.MessageInfo;
import com.sqx.modules.message.service.MessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Date;

@Service
public class VipDetailsServiceImpl extends ServiceImpl<VipDetailsDao, VipDetails> implements VipDetailsService {

    @Autowired
    private UserService userService;
    @Autowired
    private MessageService messageService;

    @Override
    public Result selectVipDetails() {
        return Result.success().put("data", baseMapper.selectList(null));
    }

    @Override
    public Result insertVipDetails(VipDetails vipDetails) {
        int cpunt = baseMapper.insert(vipDetails);
        if (cpunt > 0) {
            return Result.success("添加成功！");
        } else {
            return Result.error("添加失败");
        }
    }

    /**
     * 赠送vip
     * @param userId 用户id
     * @param vipType 1用户会员 2企业会员
     * @param num 赠送天数
     * @return
     */
    @Override
    public Result giveUserVip(Long userId, Integer vipType, Integer num) {
        UserEntity userEntity = userService.getById(userId);
        if (vipType == 1) {
            if (userEntity.getUserMonthTime() != null) {
                if (LocalDateTime.now().isAfter(userEntity.getUserMonthTime())) {
                    userEntity.setUserMonthTime(LocalDateTime.now().plusDays(num));
                } else {
                    userEntity.setUserMonthTime(userEntity.getUserMonthTime().plusDays(num));
                }
            } else {
                userEntity.setUserMonthTime(LocalDateTime.now().plusDays(num));
            }
            userEntity.setIsUserVip(1);
        } else {
            if (userEntity.getCompanyMonthTime() != null) {
                if (LocalDateTime.now().isAfter(userEntity.getCompanyMonthTime())) {
                    userEntity.setCompanyMonthTime(LocalDateTime.now().plusDays(num));
                } else {
                    userEntity.setCompanyMonthTime(userEntity.getCompanyMonthTime().plusDays(num));
                }
            } else {
                userEntity.setCompanyMonthTime(LocalDateTime.now().plusDays(num));
            }
            userEntity.setIsCompanyVip(1);
        }
        userService.updateById(userEntity);
        MessageInfo messageInfo = new MessageInfo();
        messageInfo.setUserId(userId.toString());
        messageInfo.setTitle("赠送会员");
        if (vipType == 1) {
            messageInfo.setContent("系统赠送您会员" + "共" + num + "天");
        } else {
            messageInfo.setContent("系统赠送您企业会员" + "共" + num + "天");
        }
        messageInfo.setUserName(userEntity.getUserName());
        messageInfo.setUserId(String.valueOf(userEntity.getUserId()));
        messageInfo.setCreateAt(DateUtils.format(new Date(), DateUtils.DATE_TIME_PATTERN));
        messageInfo.setIsSee("0");
        messageService.saveBody(messageInfo);
        return Result.success();
    }

    @Override
    public Result cancelUserVip(Long userId, Integer vipType) {
        UserEntity userEntity = userService.getById(userId);
        if (vipType == 1) {
            userEntity.setIsUserVip(0);
            userEntity.setUserMonthTime(LocalDateTime.now());
        }else {
            userEntity.setIsCompanyVip(0);
            userEntity.setCompanyMonthTime(LocalDateTime.now());
        }
        return userService.updateById(userEntity) ? Result.success() : Result.error();
    }
}
