package cn.iocoder.yudao.module.member.service.userpreference;

import cn.hutool.core.bean.BeanUtil;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.member.controller.admin.userpreference.vo.UserPreferencePageReqVO;
import cn.iocoder.yudao.module.member.controller.admin.userpreference.vo.UserPreferencePercentVO;
import cn.iocoder.yudao.module.member.controller.admin.userpreference.vo.UserPreferenceSaveReqVO;
import cn.iocoder.yudao.module.member.controller.app.userpreference.vo.AppUserPreferenceSaveReqVO;
import cn.iocoder.yudao.module.member.dal.dataobject.userpreference.UserPreferenceDO;
import cn.iocoder.yudao.module.member.dal.mysql.userpreference.UserPreferenceMapper;
import cn.iocoder.yudao.module.member.enums.TastePreferencesEnum;
import cn.iocoder.yudao.module.member.util.MemberConstants;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.member.enums.ErrorCodeConstants.USER_PREFERENCE_NOT_EXISTS;

/**
 * 用户偏好 Service 实现类
 *
 * @author 开发账号
 */
@Service
@Validated
public class UserPreferenceServiceImpl implements UserPreferenceService {

    @Resource
    private UserPreferenceMapper userPreferenceMapper;

    @Override
    public Long createUserPreference(UserPreferenceSaveReqVO createReqVO) {
        // 插入
        UserPreferenceDO userPreference = BeanUtils.toBean(createReqVO, UserPreferenceDO.class);
        userPreferenceMapper.insert(userPreference);
        // 返回
        return userPreference.getId();
    }

    @Override
    public void updateUserPreference(UserPreferenceSaveReqVO updateReqVO) {
        // 校验存在
        validateUserPreferenceExists(updateReqVO.getId());
        // 更新
        UserPreferenceDO updateObj = BeanUtils.toBean(updateReqVO, UserPreferenceDO.class);
        userPreferenceMapper.updateById(updateObj);
    }

    @Override
    public void deleteUserPreference(Long id) {
        // 校验存在
        validateUserPreferenceExists(id);
        // 删除
        userPreferenceMapper.deleteById(id);
    }

    private void validateUserPreferenceExists(Long id) {
        if (userPreferenceMapper.selectById(id) == null) {
            throw exception(USER_PREFERENCE_NOT_EXISTS);
        }
    }

    @Override
    public UserPreferenceDO getUserPreference(Long id) {
        return userPreferenceMapper.selectById(id);
    }

    @Override
    public PageResult<UserPreferenceDO> getUserPreferencePage(UserPreferencePageReqVO pageReqVO) {
        return userPreferenceMapper.selectPage(pageReqVO);
    }

    @Override
    public Boolean saveOrUpdate(AppUserPreferenceSaveReqVO updateReqVO) {
        UserPreferenceDO userPreferenceDO = userPreferenceMapper.selectOne(Wrappers.<UserPreferenceDO>lambdaQuery()
                .eq(UserPreferenceDO::getUserId, updateReqVO.getUserId())
                .last(MemberConstants.LIMIT_ONE));
        Boolean result = true;
        if (userPreferenceDO == null) {
            UserPreferenceDO add = new UserPreferenceDO();
            BeanUtil.copyProperties(updateReqVO, add);
            result = userPreferenceMapper.insert(add) > 0;
        } else {
            BeanUtil.copyProperties(updateReqVO, userPreferenceDO);
            result = userPreferenceMapper.updateById(userPreferenceDO) > 0;
        }
        return result;

    }

    @Override
    public UserPreferenceDO getUserPreferenceByUserId(Long userId) {
        UserPreferenceDO userPreferenceDO = userPreferenceMapper.selectOne(Wrappers.<UserPreferenceDO>lambdaQuery()
                .eq(UserPreferenceDO::getUserId, userId)
                .last(MemberConstants.LIMIT_ONE));
        return userPreferenceDO;
    }

    @Override
    public List<UserPreferencePercentVO> getPercent() {
        List<UserPreferencePercentVO> list = new ArrayList<>();
        Long count = userPreferenceMapper.selectCount();
        if (count != null && count != 0) {
            BigDecimal total = new BigDecimal(count);
            for (TastePreferencesEnum preferencesEnum : TastePreferencesEnum.values()) {
                Integer i = userPreferenceMapper.selectCountByType(preferencesEnum.getName());
                BigDecimal divide = new BigDecimal(i).divide(total,2,BigDecimal.ROUND_HALF_UP)
                        .multiply(new BigDecimal(100)).setScale(0, BigDecimal.ROUND_HALF_UP);
                UserPreferencePercentVO userPreferencePercentVO = new UserPreferencePercentVO();
                userPreferencePercentVO.setName(preferencesEnum.getName()).setPercent(divide + "%");
                list.add(userPreferencePercentVO);
            }
        }
        return list;
    }





}