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

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

import com.bonus.canteen.core.cook.domain.CookEvaluaDetail;
import com.bonus.canteen.core.cook.domain.CookEvaluaPicture;
import com.bonus.canteen.core.cook.dto.CookEvaluaOrderDTO;
import com.bonus.canteen.core.cook.mapper.CookEvaluaDetailMapper;
import com.bonus.canteen.core.cook.mapper.CookEvaluaPictureMapper;
import com.bonus.canteen.core.cook.vo.CookEvaluaOrderVo;
import com.bonus.common.core.exception.ServiceException;
import com.bonus.common.core.utils.DateUtils;
import com.bonus.common.security.utils.SecurityUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.bonus.canteen.core.cook.mapper.CookEvaluaOrderMapper;
import com.bonus.canteen.core.cook.domain.CookEvaluaOrder;
import com.bonus.canteen.core.cook.service.ICookEvaluaOrderService;

/**
 * 订单评价Service业务层处理
 * 
 * @author xsheng
 * @date 2025-05-25
 */
@Service
public class CookEvaluaOrderServiceImpl implements ICookEvaluaOrderService {
    @Autowired
    private CookEvaluaOrderMapper cookEvaluaOrderMapper;
    @Autowired
    private CookEvaluaDetailMapper cookEvaluaDetailMapper;
    @Autowired
    private CookEvaluaPictureMapper cookEvaluaPictureMapper;

    /**
     * 查询订单评价
     * 
     * @param evaluaId 订单评价主键
     * @return 订单评价
     */
    @Override
    public CookEvaluaOrder selectCookEvaluaOrderByEvaluaId(Long evaluaId) {
        return cookEvaluaOrderMapper.selectCookEvaluaOrderByEvaluaId(evaluaId);
    }

    /**
     * 查询订单评价
     *
     * @param orderId 订单评价主键
     * @return 订单评价
     */
    @Override
    public CookEvaluaOrderVo selectCookEvaluaOrderById(Long orderId) {
        CookEvaluaOrder cookEvaluaOrder = cookEvaluaOrderMapper.selectCookEvaluaOrderByOrderId(orderId);
        CookEvaluaOrderVo cookEvaluaOrderVo = new CookEvaluaOrderVo();
        BeanUtils.copyProperties(cookEvaluaOrder, cookEvaluaOrderVo);

        CookEvaluaDetail cookEvaluaDetail = new CookEvaluaDetail();
        cookEvaluaDetail.setEvaluaId(cookEvaluaOrder.getEvaluaId());
        List<CookEvaluaDetail> cookEvaluaDetails = cookEvaluaDetailMapper.selectCookEvaluaDetailList(cookEvaluaDetail);
        cookEvaluaOrderVo.setDetailList(cookEvaluaDetails);

        CookEvaluaPicture cookEvaluaPicture = new CookEvaluaPicture();
        cookEvaluaPicture.setEvaluaId(cookEvaluaOrder.getEvaluaId());
        List<CookEvaluaPicture> cookEvaluaPictures = cookEvaluaPictureMapper.selectCookEvaluaPictureList(cookEvaluaPicture);
        List<String> pictures = cookEvaluaPictures.stream().map(CookEvaluaPicture::getImgUrl).collect(Collectors.toList());
        cookEvaluaOrderVo.setPictureList(pictures);

        return cookEvaluaOrderVo;
    }

    /**
     * 查询订单评价列表
     * 
     * @param cookEvaluaOrder 订单评价
     * @return 订单评价
     */
    @Override
    public List<CookEvaluaOrder> selectCookEvaluaOrderList(CookEvaluaOrder cookEvaluaOrder) {
        List<CookEvaluaOrder> list = cookEvaluaOrderMapper.selectCookEvaluaOrderList(cookEvaluaOrder);
        for (CookEvaluaOrder evaluaOrder : list) {
            CookEvaluaDetail cookEvaluaDetail = new CookEvaluaDetail();
            cookEvaluaDetail.setEvaluaId(evaluaOrder.getEvaluaId());
            List<CookEvaluaDetail> cookEvaluaDetails = cookEvaluaDetailMapper.selectCookEvaluaDetailList(cookEvaluaDetail);
            evaluaOrder.setDetailList(cookEvaluaDetails);

            CookEvaluaPicture cookEvaluaPicture = new CookEvaluaPicture();
            cookEvaluaPicture.setEvaluaId(evaluaOrder.getEvaluaId());
            List<CookEvaluaPicture> menuEvaluaPictures = cookEvaluaPictureMapper.selectCookEvaluaPictureList(cookEvaluaPicture);
            List<String> pictures = menuEvaluaPictures.stream().map(CookEvaluaPicture::getImgUrl).collect(Collectors.toList());
            evaluaOrder.setPictureList(pictures);
        }
        return list;
    }

    /**
     * 新增订单评价
     * 
     * @param cookEvaluaOrderDTO 订单评价
     * @return 结果
     */
    @Override
    public int insertCookEvaluaOrder(CookEvaluaOrderDTO cookEvaluaOrderDTO) {
        if (Objects.isNull(cookEvaluaOrderDTO.getOrderId())) {
            throw new ServiceException("订单编号不能为空");
        }
        cookEvaluaOrderDTO.setCreateTime(DateUtils.getNowDate());
        cookEvaluaOrderDTO.setCreateBy(SecurityUtils.getUsername());
        if (Objects.isNull(cookEvaluaOrderDTO.getUserId())) {
            cookEvaluaOrderDTO.setUserId(SecurityUtils.getUserId());
        }
        try {
            CookEvaluaOrder evaluaOrder = cookEvaluaOrderMapper.selectCookEvaluaOrderByOrderId(cookEvaluaOrderDTO.getOrderId());
            int count = 0;
            long evaluaId = 0;
            if (Objects.isNull(evaluaOrder)) {
                count += cookEvaluaOrderMapper.insertCookEvaluaOrder(cookEvaluaOrderDTO);
                evaluaId = cookEvaluaOrderDTO.getEvaluaId();
            } else {
                evaluaId = evaluaOrder.getEvaluaId();
            }
            List<CookEvaluaDetail> detailList = cookEvaluaOrderDTO.getDetailList();
            for (int i = 0; i < detailList.size(); i++) {
                CookEvaluaDetail cookEvaluaDetail = detailList.get(i);
                cookEvaluaDetail.setEvaluaId(evaluaId);
                cookEvaluaDetail.setCreateTime(DateUtils.getNowDate());
                cookEvaluaDetail.setCreateBy(SecurityUtils.getUsername());
                count += cookEvaluaDetailMapper.insertCookEvaluaDetail(cookEvaluaDetail);
            }
            List<String> pictureList = cookEvaluaOrderDTO.getPictureList();
            for (int i = 0; i < pictureList.size(); i++) {
                CookEvaluaPicture cookEvaluaPicture = new CookEvaluaPicture();
                cookEvaluaPicture.setEvaluaId(evaluaId);
                cookEvaluaPicture.setImgUrl(pictureList.get(i));
                cookEvaluaPicture.setCreateTime(DateUtils.getNowDate());
                cookEvaluaPicture.setCreateBy(SecurityUtils.getUsername());
                count += cookEvaluaPictureMapper.insertCookEvaluaPicture(cookEvaluaPicture);
            }
            cookEvaluaOrderMapper.updateOrderEvaluaStatus(cookEvaluaOrderDTO.getOrderId());
            return count;
        } catch (Exception e) {
            throw new ServiceException("新增订单评价异常, " + e.getMessage());
        }
    }

    /**
     * 修改订单评价
     * 
     * @param cookEvaluaOrder 订单评价
     * @return 结果
     */
    @Override
    public int updateCookEvaluaOrder(CookEvaluaOrder cookEvaluaOrder) {
        cookEvaluaOrder.setUpdateTime(DateUtils.getNowDate());
        try {
            return cookEvaluaOrderMapper.updateCookEvaluaOrder(cookEvaluaOrder);
        } catch (Exception e) {
            throw new ServiceException(e.getMessage());
        }
    }

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

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