/*
 * Copyright (c) 2018-2999 广州亚米信息科技有限公司 All rights reserved.
 *
 * https://www.gz-yami.com/
 *
 * 未经允许，不可做商业用途！
 *
 * 版权所有，侵权必究！
 */

package com.yami.shop.service.impl;


import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yami.shop.bean.app.dto.ProdCommDataDto;
import com.yami.shop.bean.app.dto.ProdCommDto;
import com.yami.shop.bean.app.param.ProdCommParam;
import com.yami.shop.bean.model.OrderItem;
import com.yami.shop.bean.model.ProdComm;
import com.yami.shop.common.util.Arith;
import com.yami.shop.common.util.HttpContextUtils;
import com.yami.shop.common.util.IPHelper;
import com.yami.shop.dao.OrderItemMapper;
import com.yami.shop.dao.ProdCommMapper;
import com.yami.shop.service.ProdCommService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.Objects;

/**
 * 商品评论
 *
 * @author xwc
 * @date 2019-04-19 10:43:57
 */
@Service
@AllArgsConstructor
public class ProdCommServiceImpl extends ServiceImpl<ProdCommMapper, ProdComm> implements ProdCommService {

    private final ProdCommMapper prodCommMapper;

    private final OrderItemMapper orderItemMapper;

    @Override
    public ProdCommDataDto getProdCommDataByProdId(Long prodId) {
        ProdCommDataDto prodCommDataDto=prodCommMapper.getProdCommDataByProdId(prodId);
        //计算出好评率
        if(prodCommDataDto.getPraiseNumber() == 0||prodCommDataDto.getNumber() == 0){
            prodCommDataDto.setPositiveRating(0.0);
        }else{
//            prodCommDataDto.setPositiveRating(Arith.mul(Arith.div(prodCommDataDto.getPraiseNumber(),prodCommDataDto.getNumber()),100));
            prodCommDataDto.setPositiveRating(Arith.div(Arith.mul(prodCommDataDto.getPraiseNumber(),100),prodCommDataDto.getNumber(),2));
        }
        return prodCommDataDto;
    }

    @Override
    public IPage<ProdCommDto> getProdCommDtoPageByUserId(Page page, String userId) {
        return prodCommMapper.getProdCommDtoPageByUserId(page,userId);
    }

    @Override
    public IPage<ProdCommDto> getProdCommDtoPageByProdId(Page page, Long prodId, Integer evaluate) {

        IPage<ProdCommDto> prodCommDtos = prodCommMapper.getProdCommDtoPageByProdId(page, prodId, evaluate);
        prodCommDtos.getRecords().forEach(prodCommDto -> {
            // 匿名评价
            if (prodCommDto.getIsAnonymous() == 1) {
                prodCommDto.setNickName(null);
                prodCommDto.setPic(null);
            }
        });
        return prodCommDtos;
    }

    @Override
    public IPage<ProdComm> getProdCommPage(Page page,ProdComm prodComm,Long shopId) {
        return prodCommMapper.getProdCommPage(page,prodComm,shopId);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void comm(OrderItem orderItem, ProdCommParam prodCommParam) {
        // 插入评论
        ProdComm prodComm = new ProdComm();
        prodComm.setProdId(orderItem.getProdId());
        prodComm.setOrderItemId(orderItem.getOrderItemId());
        prodComm.setUserId(orderItem.getUserId());
        prodComm.setScore(prodCommParam.getScore());
        prodComm.setContent(prodCommParam.getContent());
//        prodComm.setPics(prodCommParam.getPics());
        prodComm.setPics(StrUtil.isBlank(prodCommParam.getPics()) ?null:prodCommParam.getPics());
        prodComm.setIsAnonymous(prodCommParam.getIsAnonymous());
        prodComm.setRecTime(new Date());
        // 平台不需要控评
        prodComm.setStatus(1);
        prodComm.setPostip(IPHelper.getIpAddr());
        // 好评
        if (Objects.equals(prodComm.getScore(), 5) || Objects.equals(prodComm.getScore(), 4)) {
            prodComm.setEvaluate(0);
        }
        // 中评
        else if(Objects.equals(prodComm.getScore(), 3)) {
            prodComm.setEvaluate(1);
        }
        // 差评
        else if(Objects.equals(prodComm.getScore(), 1) || Objects.equals(prodComm.getScore(), 2)) {
            prodComm.setEvaluate(2);
        }
        prodCommMapper.insert(prodComm);

        // 更新订单项评论状态
        orderItem.setCommSts(1);
        orderItemMapper.updateById(orderItem);
    }
}
