package com.younike.system.service.impl;

import com.younike.system.domain.Goods;
import com.younike.system.domain.GoodsCommentDimension;
import com.younike.system.domain.vo.CateDimensionVo;
import com.younike.system.domain.vo.CommentDimensionVo;
import com.younike.system.mapper.CateDimensionMapper;
import com.younike.system.mapper.GoodsCommentDimensionMapper;
import com.younike.system.mapper.GoodsMapper;
import com.younike.system.service.IGoodsCommentDimensionService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;

/**
 * 【请填写功能名称】Service业务层处理
 * 
 * @author elev
 * @date 2022-01-06
 */
@Service
public class GoodsCommentDimensionServiceImpl implements IGoodsCommentDimensionService {
    @Autowired
    private GoodsCommentDimensionMapper goodsCommentDimensionMapper;

    @Autowired
    private CateDimensionMapper cateDimensionMapper;

    @Autowired
    private GoodsMapper goodsMapper;


    /**
     * 查询【请填写功能名称】
     * 
     * @param id 【请填写功能名称】主键
     * @return 【请填写功能名称】
     */
    @Override
    public GoodsCommentDimension selectGoodsCommentDimensionById(Long id) {
        return goodsCommentDimensionMapper.selectGoodsCommentDimensionById(id);
    }

    /**
     * 查询【请填写功能名称】列表
     * 
     * @param goodsCommentDimension 【请填写功能名称】
     * @return 【请填写功能名称】
     */
    @Override
    public List<GoodsCommentDimension> selectGoodsCommentDimensionList(GoodsCommentDimension goodsCommentDimension) {
        return goodsCommentDimensionMapper.selectGoodsCommentDimensionList(goodsCommentDimension);
    }

    /**
     * 新增【请填写功能名称】
     * 
     * @param goodsCommentDimension 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int insertGoodsCommentDimension(GoodsCommentDimension goodsCommentDimension) {
        goodsCommentDimension.setCreateTime(LocalDateTime.now());
        return goodsCommentDimensionMapper.insertGoodsCommentDimension(goodsCommentDimension);
    }

    /**
     * 修改【请填写功能名称】
     * 
     * @param goodsCommentDimension 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int updateGoodsCommentDimension(GoodsCommentDimension goodsCommentDimension) {
        goodsCommentDimension.setUpdateTime(LocalDateTime.now());
        return goodsCommentDimensionMapper.updateGoodsCommentDimension(goodsCommentDimension);
    }

    /**
     * 批量删除【请填写功能名称】
     * 
     * @param ids 需要删除的【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteGoodsCommentDimensionByIds(Long[] ids) {
        return goodsCommentDimensionMapper.deleteGoodsCommentDimensionByIds(ids);
    }

    /**
     * 删除【请填写功能名称】信息
     * 
     * @param id 【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteGoodsCommentDimensionById(Long id)
    {
        return goodsCommentDimensionMapper.deleteGoodsCommentDimensionById(id);
    }

    /**
     * 通过分类id和产品id查询评价维度信息
     *
     * @param categoryId
     * @param goodsId
     * @return
     */
    @Override
    public CommentDimensionVo getCommentDimensionInfo(Long categoryId, Long goodsId) {
        CommentDimensionVo commentDimensionVo = new CommentDimensionVo();
        // 查询分类维度列表
        List<CateDimensionVo> cateDimensionVoList = cateDimensionMapper.listByCategoryId(categoryId);
        if (CollectionUtils.isNotEmpty(cateDimensionVoList)) {
            for (CateDimensionVo cateDimensionVo : cateDimensionVoList) {
               cateDimensionVo.setDimensionScore(0);
            }
            commentDimensionVo.setCateDimensionVoList(cateDimensionVoList);
        }
        // 查询评价可得优金豆数量
        Goods goods = goodsMapper.selectGoodsByGoodsId(goodsId);
        if (goods != null) {
            String coinStr = goods.getCoin() == null ? "0" : String.valueOf(goods.getCoin());
            int coin = Integer.parseInt(coinStr);
            if (coin <= 0) {
                commentDimensionVo.setCommentCoin(BigDecimal.ZERO);
                commentDimensionVo.setVideoImageCoin(BigDecimal.ZERO);
            } else {
                if (coin % 2 == 0) {
                    commentDimensionVo.setCommentCoin(BigDecimal.valueOf(coin/2));
                    commentDimensionVo.setVideoImageCoin(BigDecimal.valueOf(coin/2));
                } else {
                    commentDimensionVo.setCommentCoin(BigDecimal.ONE.add(BigDecimal.valueOf(coin/2)));
                    commentDimensionVo.setVideoImageCoin(BigDecimal.valueOf(coin/2));
                }
            }
        }

        return commentDimensionVo;
    }
}
