package com.bonus.canteen.core.menu.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

import com.bonus.canteen.core.menu.domain.MenuEvaluaDetail;
import com.bonus.canteen.core.menu.domain.MenuEvaluaPicture;
import com.bonus.canteen.core.menu.dto.MenuEvaluaOrderAddDTO;
import com.bonus.canteen.core.menu.mapper.MenuEvaluaDetailMapper;
import com.bonus.canteen.core.menu.mapper.MenuEvaluaPictureMapper;
import com.bonus.canteen.core.menu.vo.MenuEvaluaOrderVo;
import com.bonus.common.core.exception.ServiceException;
import com.bonus.common.core.utils.DateUtils;
import com.bonus.common.security.utils.SecurityUtils;
import io.swagger.annotations.ApiModelProperty;
import org.apache.commons.collections.list.AbstractLinkedList;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.bonus.canteen.core.menu.mapper.MenuEvaluaOrderMapper;
import com.bonus.canteen.core.menu.domain.MenuEvaluaOrder;
import com.bonus.canteen.core.menu.service.IMenuEvaluaOrderService;

/**
 * 订单评价Service业务层处理
 * 
 * @author xsheng
 * @date 2025-04-20
 */
@Service
public class MenuEvaluaOrderServiceImpl implements IMenuEvaluaOrderService {
    @Autowired
    private MenuEvaluaOrderMapper menuEvaluaOrderMapper;
    @Autowired
    private MenuEvaluaPictureMapper menuEvaluaPictureMapper;
    @Autowired
    private MenuEvaluaDetailMapper menuEvaluaDetailMapper;

    /**
     * 查询订单评价
     * 
     * @param orderId 订单评价主键
     * @return 订单评价
     */
    @Override
    public MenuEvaluaOrderVo selectMenuEvaluaOrderById(Long orderId) {
        MenuEvaluaOrder menuEvaluaOrder = menuEvaluaOrderMapper.selectMenuEvaluaOrderById(orderId);
        MenuEvaluaOrderVo menuEvaluaOrderVo = new MenuEvaluaOrderVo();
        BeanUtils.copyProperties(menuEvaluaOrder, menuEvaluaOrderVo);

        MenuEvaluaDetail menuEvaluaDetail = new MenuEvaluaDetail();
        menuEvaluaDetail.setEvaluaId(menuEvaluaOrder.getEvaluaId());
        List<MenuEvaluaDetail> menuEvaluaDetails = menuEvaluaDetailMapper.selectMenuEvaluaDetailList(menuEvaluaDetail);
        menuEvaluaOrderVo.setDetailList(menuEvaluaDetails);

        MenuEvaluaPicture menuEvaluaPicture = new MenuEvaluaPicture();
        menuEvaluaPicture.setEvaluaId(menuEvaluaOrder.getEvaluaId());
        List<MenuEvaluaPicture> menuEvaluaPictures = menuEvaluaPictureMapper.selectMenuEvaluaPictureList(menuEvaluaPicture);
        List<String> pictures = menuEvaluaPictures.stream().map(MenuEvaluaPicture::getPictureUrl).collect(Collectors.toList());
        menuEvaluaOrderVo.setPictureList(pictures);

        return menuEvaluaOrderVo;
    }

    /**
     * 查询订单评价列表
     * 
     * @param menuEvaluaOrder 订单评价
     * @return 订单评价
     */
    @Override
    public List<MenuEvaluaOrder> selectMenuEvaluaOrderList(MenuEvaluaOrder menuEvaluaOrder) {
        return menuEvaluaOrderMapper.selectMenuEvaluaOrderList(menuEvaluaOrder);
    }

    /**
     * 新增订单评价
     * 
     * @param menuEvaluaOrder 订单评价
     * @return 结果
     */
    @Override
    public int insertMenuEvaluaOrder(MenuEvaluaOrderAddDTO menuEvaluaOrder) {
        if (Objects.isNull(menuEvaluaOrder.getOrdId())) {
            throw new ServiceException("订单编号不能为空");
        }
        menuEvaluaOrder.setCreateTime(DateUtils.getNowDate());
        menuEvaluaOrder.setCreateBy(SecurityUtils.getUsername());
        try {
            int count = menuEvaluaOrderMapper.insertMenuEvaluaOrder(menuEvaluaOrder);
            long evaluaId = menuEvaluaOrder.getEvaluaId();
            List<String> pictureList = menuEvaluaOrder.getPictureList();
            for (int i = 0; i < pictureList.size(); i++) {
                MenuEvaluaPicture menuEvaluaPicture = new MenuEvaluaPicture();
                menuEvaluaPicture.setEvaluaId(evaluaId);
                menuEvaluaPicture.setPictureUrl(pictureList.get(i));
                menuEvaluaPicture.setCreateTime(DateUtils.getNowDate());
                menuEvaluaPicture.setCreateBy(SecurityUtils.getUsername());
                menuEvaluaPictureMapper.insertMenuEvaluaPicture(menuEvaluaPicture);
            }
            List<MenuEvaluaDetail> detailList = menuEvaluaOrder.getDetailList();
            for (int i = 0; i < detailList.size(); i++) {
                MenuEvaluaDetail menuEvaluaDetail = detailList.get(i);
                menuEvaluaDetail.setEvaluaId(evaluaId);
                menuEvaluaDetail.setCreateTime(DateUtils.getNowDate());
                menuEvaluaDetail.setCreateBy(SecurityUtils.getUsername());
                menuEvaluaDetailMapper.insertMenuEvaluaDetail(menuEvaluaDetail);
            }
            menuEvaluaOrderMapper.updateOrderEvaluaStatus(menuEvaluaOrder.getOrdId());
            return count;
        } catch (Exception e) {
            throw new ServiceException("新增订单评价异常, " + e.getMessage());
        }
    }

    /**
     * 修改订单评价
     * 
     * @param menuEvaluaOrder 订单评价
     * @return 结果
     */
    @Override
    public int updateMenuEvaluaOrder(MenuEvaluaOrder menuEvaluaOrder) {
        menuEvaluaOrder.setUpdateTime(DateUtils.getNowDate());
        try {
            return menuEvaluaOrderMapper.updateMenuEvaluaOrder(menuEvaluaOrder);
        } catch (Exception e) {
            throw new ServiceException("更新订单评价异常, " + e.getMessage());
        }
    }

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

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