package com.ruoyi.mall.service.impl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSONArray;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.mall.domain.*;
import com.ruoyi.mall.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import com.ruoyi.mall.mapper.MallOrderCommentMapper;
import org.springframework.transaction.annotation.Transactional;

/**
 * 商品评论Service业务层处理
 * 
 * @author chang
 * @date 2021-11-25
 */
@Service
public class MallOrderCommentServiceImpl implements IMallOrderCommentService 
{
    @Autowired
    private MallOrderCommentMapper mallOrderCommentMapper;

    @Autowired
    IMallOrderOrderService orderOrderService;

    @Autowired
    IMallGoodsProductService productService;

    @Autowired
    IMallOrderGoodsService orderGoodsService;

    @Autowired
    IMallImgService imgService;

    /**
     * 查询商品评论
     * 
     * @param id 商品评论主键
     * @return 商品评论
     */
    @Override
    public MallOrderComment selectMallOrderCommentById(Long id)
    {
        return mallOrderCommentMapper.selectMallOrderCommentById(id);
    }

    /**
     * 查询商品评论列表
     * 
     * @param mallOrderComment 商品评论
     * @return 商品评论
     */
    @Override
    public List<Map<String, Object>> selectMallOrderCommentList(MallOrderComment mallOrderComment)
    {
        return mallOrderCommentMapper.selectMallOrderCommentList(mallOrderComment);
    }

    /**
     * 新增商品评论
     * 
     * @param mallOrderComment 商品评论
     * @return 结果
     */
    @Override
    public int insertMallOrderComment(MallOrderComment mallOrderComment)
    {
        return mallOrderCommentMapper.insertMallOrderComment(mallOrderComment);
    }

    /**
     * 修改商品评论
     * 
     * @param mallOrderComment 商品评论
     * @return 结果
     */
    @Override
    public int updateMallOrderComment(MallOrderComment mallOrderComment)
    {
        return mallOrderCommentMapper.updateMallOrderComment(mallOrderComment);
    }

    /**
     * 批量删除商品评论
     * 
     * @param ids 需要删除的商品评论主键
     * @return 结果
     */
    @Override
    public int deleteMallOrderCommentByIds(Long[] ids)
    {
        return mallOrderCommentMapper.deleteMallOrderCommentByIds(ids);
    }

    /**
     * 删除商品评论信息
     * 
     * @param id 商品评论主键
     * @return 结果
     */
    @Override
    public int deleteMallOrderCommentById(Long id)
    {
        return mallOrderCommentMapper.deleteMallOrderCommentById(id);
    }

    /*
    * 评价订单
    * */
    @Transactional
    @Override
    public int commentOrder(Map<String, Object> map) {

        try {
            String commentListStr = map.get("commentList").toString();
            List<Map<String, Object>> list = (List<Map<String, Object>>) JSONArray.parse(commentListStr);
            for (Map<String, Object> commentMap : list) {
                String productId = commentMap.get("productId").toString();
                if (!StringUtils.isEmpty(productId)){
                    MallGoodsProduct pro = productService.selectMallGoodsProductById(Long.parseLong(productId));
                    MallOrderComment comment = new MallOrderComment();
                    comment.setOrderId(Long.parseLong(map.get("orderId").toString()));
                    comment.setProductId(pro.getId());
                    comment.setGoodsId(pro.getGoodsId());
                    comment.setContext(commentMap.get("context").toString());
                    comment.setUserId(Long.parseLong(map.get("userId").toString()));
                    comment.setCreateAt(DateUtils.getTime());
                    mallOrderCommentMapper.insertMallOrderComment(comment);

                    //图片处理
                    String urls = commentMap.get("urls").toString();
                    if (!StringUtils.isEmpty(urls)){
                        urls = urls.replace("[","");
                        urls = urls.replace("]","");
                        urls = urls.replace("\"","");
                        String[] urlList = urls.split(",");
                        for (String urlStr : urlList) {
                            MallImg img = new MallImg();
                            img.setResId(comment.getId());
                            img.setType("2");
                            img.setImg(urlStr);
                            img.setCreateAt(DateUtils.getTime());
                            imgService.insertMallImg(img);
                        }
                    }

                    MallOrderGoods orderGoods = new MallOrderGoods();
                    orderGoods.setProductId(Long.parseLong(productId));
                    orderGoods.setOrderId(Long.parseLong(map.get("orderId").toString()));
                    orderGoods.setGoodsId(pro.getGoodsId());
                    List<MallOrderGoods> goodsList = orderGoodsService.selectMallOrderGoodsList(orderGoods);
                    for (MallOrderGoods goods : goodsList) {
                        goods.setComment("1");
                        orderGoodsService.updateMallOrderGoods(goods);
                    }
                }
            }
            MallOrderOrder orgOrder = orderOrderService.selectMallOrderOrderById(Long.parseLong(map.get("orderId").toString()));
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            orgOrder.setCompleteAt(formatter.format(new Date()));
            orgOrder.setStatus("4");
            orderOrderService.updateMallOrderOrder(orgOrder);
            return 1;
        } catch (Exception e) {
            return 0;
        }
    }

    @Override
    public int selectCountByGoodsId(Long goodsId) {
        return mallOrderCommentMapper.selectCountByGoodsId(goodsId);
    }

    @Override
    public List<Map<String, Object>> selectTwoRecordByGoodsId(Long goodsId) {
        return mallOrderCommentMapper.selectTwoRecordByGoodsId(goodsId);
    }

    @Override
    public List<Map<String, Object>> selectCommentListByGoodsId(Long goodsId) {
        return mallOrderCommentMapper.selectCommentListByGoodsId(goodsId);
    }


}
