package cn.newfeifan.mall.module.distri.service.usercollectbefore;

import cn.newfeifan.mall.module.distri.controller.app.usercollectbefore.vo.UserCollectBeforePageReqVO;
import cn.newfeifan.mall.module.distri.controller.app.usercollectbefore.vo.UserCollectBeforeSaveReqVO;
import cn.newfeifan.mall.module.distri.dal.dataobject.orderpercentage.OrderPercentageDO;
import cn.newfeifan.mall.module.distri.enums.SocialStatusEnum;
import cn.newfeifan.mall.module.distri.service.duser.DuserService;
import cn.newfeifan.mall.module.distri.service.orderpercentage.OrderPercentageService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import cn.newfeifan.mall.module.distri.dal.dataobject.usercollectbefore.UserCollectBeforeDO;
import cn.newfeifan.mall.framework.common.pojo.PageResult;
import cn.newfeifan.mall.framework.common.util.object.BeanUtils;

import cn.newfeifan.mall.module.distri.dal.mysql.usercollectbefore.UserCollectBeforeMapper;

import static cn.newfeifan.mall.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.newfeifan.mall.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;
import static cn.newfeifan.mall.module.distri.enums.ErrorCodeConstants.*;

/**
 * 用户曾经收藏过什么商品的 Service 实现类
 *
 * @author 非繁人
 */
@Service
@Validated
public class UserCollectBeforeServiceImpl implements UserCollectBeforeService {

    @Resource
    private UserCollectBeforeMapper userCollectBeforeMapper;

    @Resource
    private OrderPercentageService orderPercentageService;

    @Resource
    private DuserService duserService;

    @Override
    public Long createUserCollectBefore(UserCollectBeforeSaveReqVO createReqVO) {
        createReqVO.setUserId(getLoginUserId());
        // 插入
        UserCollectBeforeDO userCollectBefore = BeanUtils.toBean(createReqVO, UserCollectBeforeDO.class);

        UserCollectBeforeDO userCollectBeforeDO = userCollectBeforeMapper.selectOne(new LambdaQueryWrapper<UserCollectBeforeDO>()
                .eq(UserCollectBeforeDO::getProductSpuId, createReqVO.getProductSpuId())
                .eq(UserCollectBeforeDO::getUserId, createReqVO.getUserId())
        );

        if(userCollectBeforeDO != null){
            return userCollectBeforeDO.getId();
        }

        //没有收藏过这个商品则获得身价
        OrderPercentageDO orderPercentageDO = orderPercentageService.queryStatus();
        Long collectSocialStatus = Long.valueOf(orderPercentageDO.getCollectSocialStatus());
        duserService.updateDuserSocial(createReqVO.getUserId(), collectSocialStatus, SocialStatusEnum.FAVORITE_SOCIAL.getStatus());

        userCollectBeforeMapper.insert(userCollectBefore);
        // 返回
        return userCollectBefore.getId();
    }

    @Override
    public void updateUserCollectBefore(UserCollectBeforeSaveReqVO updateReqVO) {
        // 校验存在
        validateUserCollectBeforeExists(updateReqVO.getId());
        // 更新
        UserCollectBeforeDO updateObj = BeanUtils.toBean(updateReqVO, UserCollectBeforeDO.class);
        userCollectBeforeMapper.updateById(updateObj);
    }

    @Override
    public void deleteUserCollectBefore(Long id) {
        // 校验存在
        validateUserCollectBeforeExists(id);
        // 删除
        userCollectBeforeMapper.deleteById(id);
    }

    private void validateUserCollectBeforeExists(Long id) {
        if (userCollectBeforeMapper.selectById(id) == null) {
            throw exception(USER_COLLECT_BEFORE_NOT_EXISTS);
        }
    }

    @Override
    public UserCollectBeforeDO getUserCollectBefore(Long id) {
        return userCollectBeforeMapper.selectById(id);
    }

    @Override
    public PageResult<UserCollectBeforeDO> getUserCollectBeforePage(UserCollectBeforePageReqVO pageReqVO) {
        return userCollectBeforeMapper.selectPage(pageReqVO);
    }

}