package com.fs.user.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.fs.common.exception.ServiceException;
import com.fs.common.utils.DateUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fs.common.utils.StringUtils;
import com.fs.hisStore.domain.FsStoreOrderScrm;
import com.fs.hisStore.service.IFsStoreOrderScrmService;
import com.fs.store.vo.ProductScrmListVO;
import com.fs.user.domain.param.FsUserComplaintParam;
import com.fs.user.domain.vo.FsUserComplaintVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.fs.user.mapper.FsUserComplaintMapper;
import com.fs.user.domain.FsUserComplaint;
import com.fs.user.service.IFsUserComplaintService;

/**
 * 用户投诉Service业务层处理
 *
 * @author fs
 * @date 2025-08-27
 */
@Service
public class FsUserComplaintServiceImpl extends ServiceImpl<FsUserComplaintMapper, FsUserComplaint> implements IFsUserComplaintService {

    @Autowired
    IFsStoreOrderScrmService fsStoreOrderScrmService;

    /**
     * 查询用户投诉
     *
     * @param id 用户投诉主键
     * @return 用户投诉
     */
    @Override
    public FsUserComplaintVo selectFsUserComplaintVoById(Long id)
    {
        return baseMapper.selectFsUserComplaintVoById(id);
    }

    /**
     * 查询用户投诉列表
     *
     * @param fsUserComplaint 用户投诉
     * @return 用户投诉
     */
    @Override
    public List<FsUserComplaint> selectFsUserComplaintList(FsUserComplaint fsUserComplaint)
    {
        return baseMapper.selectFsUserComplaintList(fsUserComplaint);
    }

    /**
     * 查询用户投诉列表
     *
     * @param param 用户投诉
     * @return 用户投诉
     */
    @Override
    public List<FsUserComplaintVo> selectFsUserComplaintVoList(FsUserComplaintParam param)
    {
        return baseMapper.selectFsUserComplaintVoList(param);
    }

    /**
     * 新增用户投诉
     *
     * @param fsUserComplaint 用户投诉
     * @return 结果
     */
    @Override
    public int insertFsUserComplaint(FsUserComplaint fsUserComplaint)
    {
        fsUserComplaint.setIsHandleStore(0);
        fsUserComplaint.setIsHandlePlatform(0);
        fsUserComplaint.setIsReadUser(0);
        fsUserComplaint.setIsReadStore(1);
        fsUserComplaint.setIsReadPlatform(1);
        fsUserComplaint.setCreateTime(DateUtils.getNowDate());
        if(null != fsUserComplaint.getOrderId()){
            FsStoreOrderScrm fsStoreOrderScrm = fsStoreOrderScrmService.selectFsStoreOrderById(fsUserComplaint.getOrderId());
            fsUserComplaint.setStoreId(fsStoreOrderScrm.getStoreId());
        }
        return baseMapper.insertFsUserComplaint(fsUserComplaint);
    }

    /**
     * 修改用户投诉
     *
     * @param fsUserComplaint 用户投诉
     * @return 结果
     */
    @Override
    public int updateFsUserComplaint(FsUserComplaint fsUserComplaint)
    {
        //验证数据是否存在
        FsUserComplaint checkUserComplaint = baseMapper.selectFsUserComplaintById(fsUserComplaint.getId());
        if(checkUserComplaint == null){
            throw new ServiceException("操作失败，投诉信息不存在！");
        }
        fsUserComplaint.setUpdateTime(DateUtils.getNowDate());
        return baseMapper.updateFsUserComplaint(fsUserComplaint);
    }

    /**
     * 批量删除用户投诉
     *
     * @param ids 需要删除的用户投诉主键
     * @return 结果
     */
    @Override
    public int deleteFsUserComplaintByIds(Long[] ids)
    {
        return baseMapper.deleteFsUserComplaintByIds(ids);
    }

    /**
     * 删除用户投诉信息
     *
     * @param id 用户投诉主键
     * @return 结果
     */
    @Override
    public int deleteFsUserComplaintById(Long id)
    {
        return baseMapper.deleteFsUserComplaintById(id);
    }

    /**
     * 根据订单获取订单中的药品列表
     * @param orderId
     * @return
     */
    @Override
    public List<ProductScrmListVO> getProductListByOrderId(Long orderId){
        List<ProductScrmListVO> result = new ArrayList<>();
        FsStoreOrderScrm fsStoreOrderScrm = fsStoreOrderScrmService.selectFsStoreOrderById(orderId);
        if(null != fsStoreOrderScrm) {
            String itemJson = fsStoreOrderScrm.getItemJson();
            if (StringUtils.isNotBlank(itemJson)) {
                JSONArray objects = JSONArray.parseArray(itemJson);
                for (Object item : objects) {
                    JSONObject data =  (JSONObject) item;
                    JSONObject itemObj = data.getJSONObject("jsonInfo");
                    ProductScrmListVO addItem = new ProductScrmListVO();
                    addItem.setProductid(itemObj.getLong("productId"));
                    addItem.setProductname(itemObj.getString("productName"));
                    result.add(addItem);
                }
            }
        }

        return result;
    }

    @Override
    public int updateComplaintById(Long complaintId, Long storeId) {
        return baseMapper.updateComplaintById(complaintId,storeId);
    }
}

