package com.fs.his.service.impl;

import java.util.List;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.fs.common.utils.DateUtils;
import com.fs.his.domain.FsStoreOrder;
import com.fs.his.domain.FsStoreOrderScrmComment;
import com.fs.his.mapper.FsStoreOrderMapper;
import com.fs.his.mapper.FsStoreOrderScrmCommentMapper;
import com.fs.his.service.IFsStoreOrderScrmCommentService;
import com.fs.his.vo.ProductAndStroreVO;
import com.fs.hisStore.domain.FsStoreOrderScrm;
import com.fs.hisStore.enums.OrderInfoEnum;
import com.fs.hisStore.mapper.FsStoreOrderScrmMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 订单评价Service业务层处理
 * 
 * @author fs
 * @date 2025-10-15
 */
@Service
public class FsStoreOrderScrmCommentServiceImpl implements IFsStoreOrderScrmCommentService {

    @Autowired
    FsStoreOrderScrmCommentMapper fsStoreOrderScrmCommentMapper;

    @Autowired
    FsStoreOrderMapper fsStoreOrderMapper;

    @Autowired
    FsStoreOrderScrmMapper fsStoreOrderScrmMapper;
    /**
     * 查询订单评价
     * 
     * @param commentId 订单评价主键
     * @return 订单评价
     */
    @Override
    public FsStoreOrderScrmComment selectFsStoreOrderScrmCommentByCommentId(Long commentId)
    {
        return fsStoreOrderScrmCommentMapper.selectFsStoreOrderScrmCommentByCommentId(commentId);
    }

    /**
     * 查询订单评价列表
     * 
     * @param fsStoreOrderScrmComment 订单评价
     * @return 订单评价
     */
    @Override
    public List<FsStoreOrderScrmComment> selectFsStoreOrderScrmCommentList(FsStoreOrderScrmComment fsStoreOrderScrmComment)
    {
        return fsStoreOrderScrmCommentMapper.selectFsStoreOrderScrmCommentList(fsStoreOrderScrmComment);
    }

    /**
     * 新增订单评价
     * 
     * @param fsStoreOrderScrmComment 订单评价
     * @return 结果
     */
    @Override
    public int insertFsStoreOrderScrmComment(FsStoreOrderScrmComment fsStoreOrderScrmComment)
    {
        fsStoreOrderScrmComment.setCreateTime(DateUtils.getNowDate());
        //根据订单处理评价包含商品、店铺信息
        ProductAndStroreVO poductAndStroreVO = fsStoreOrderMapper.getProductAndStroreInfoByOrderId(fsStoreOrderScrmComment.getOrderId());
        if(null != poductAndStroreVO){
            fsStoreOrderScrmComment.setStoreId(poductAndStroreVO.getStoreId());
            fsStoreOrderScrmComment.setStoreName(poductAndStroreVO.getStoreName());
            String itemJson = poductAndStroreVO.getItemJson();
            JSONArray objects = JSONArray.parseArray(itemJson);
            StringBuilder ids = new StringBuilder();
            for (int i = 0; i < objects.size(); i++) {
                JSONObject jsonObject = objects.getJSONObject(i);
                ids.append(jsonObject.getString("productId")).append(",");
            }
            ids.deleteCharAt(ids.length()-1);
            fsStoreOrderScrmComment.setProductIds(ids.toString());
            fsStoreOrderScrmCommentMapper.insertFsStoreOrderScrmComment(fsStoreOrderScrmComment);
            FsStoreOrderScrm fsStoreOrderScrm = new FsStoreOrderScrm();;
            fsStoreOrderScrm.setId(fsStoreOrderScrmComment.getOrderId());
            fsStoreOrderScrm.setStatus(OrderInfoEnum.STATUS_4.getValue());
            return fsStoreOrderScrmMapper.updateFsStoreOrder(fsStoreOrderScrm);
        }
        return 0;
    }

    /**
     * 修改订单评价
     * 
     * @param fsStoreOrderScrmComment 订单评价
     * @return 结果
     */
    @Override
    public int updateFsStoreOrderScrmComment(FsStoreOrderScrmComment fsStoreOrderScrmComment)
    {
        fsStoreOrderScrmComment.setUpdateTime(DateUtils.getNowDate());
        return fsStoreOrderScrmCommentMapper.updateFsStoreOrderScrmComment(fsStoreOrderScrmComment);
    }

    /**
     * 批量删除订单评价
     * 
     * @param commentIds 需要删除的订单评价主键
     * @return 结果
     */
    @Override
    public int deleteFsStoreOrderScrmCommentByCommentIds(Long[] commentIds)
    {
        return fsStoreOrderScrmCommentMapper.deleteFsStoreOrderScrmCommentByCommentIds(commentIds);
    }

    /**
     * 删除订单评价信息
     * 
     * @param commentId 订单评价主键
     * @return 结果
     */
    @Override
    public int deleteFsStoreOrderScrmCommentByCommentId(Long commentId)
    {
        return fsStoreOrderScrmCommentMapper.deleteFsStoreOrderScrmCommentByCommentId(commentId);
    }

    /**
     * 用户端查询评价数据
     * @param fsStoreOrderScrmComment
     * @return
     */
    @Override
    public  List<FsStoreOrderScrmComment> selectFsStoreOrderScrmCommentByUser(FsStoreOrderScrmComment fsStoreOrderScrmComment){
        return fsStoreOrderScrmCommentMapper.selectFsStoreOrderScrmCommentByUser(fsStoreOrderScrmComment);
    }

    /**
     * 评价管理端查询评价数据
     * @param fsStoreOrderScrmComment
     * @return
     */
    @Override
    public  List<FsStoreOrderScrmComment> selectFsStoreOrderScrmCommentByManager(FsStoreOrderScrmComment fsStoreOrderScrmComment){
        return fsStoreOrderScrmCommentMapper.selectFsStoreOrderScrmCommentByManager(fsStoreOrderScrmComment);
    }

    /**
     * 根据订单查询评价
     * @param orderId
     * @return
     */
    @Override
    public FsStoreOrderScrmComment getCommentByOrderId(Long orderId){
        FsStoreOrderScrmComment fsStoreOrderScrmComment = new FsStoreOrderScrmComment();
        fsStoreOrderScrmComment.setOrderId(orderId);
        List<FsStoreOrderScrmComment> datas = fsStoreOrderScrmCommentMapper.selectFsStoreOrderScrmCommentByManager(fsStoreOrderScrmComment);
        if(null != datas && datas.size() > 0){
            return datas.get(0);
        }
        return null;
    }
}
