package com.xinelu.applet.service.apporderevaluate.impl;

import com.xinelu.applet.dto.apporderevaluate.OrderEvaluateAndPictureDTO;
import com.xinelu.applet.mapper.apporderevaluate.AppOrderEvaluateMapper;
import com.xinelu.applet.service.apporderevaluate.IAppOrderEvaluateService;
import com.xinelu.common.config.XinELuConfig;
import com.xinelu.common.core.domain.AjaxResult;
import com.xinelu.common.enums.GooodsOrderStatusEnum;
import com.xinelu.common.enums.OrderSourceEnum;
import com.xinelu.common.enums.OrderStatusEnum;
import com.xinelu.common.enums.OrderTypeEnum;
import com.xinelu.common.exception.ServiceException;
import com.xinelu.common.utils.bean.BeanUtils;
import com.xinelu.common.utils.file.FileUploadUtils;
import com.xinelu.common.utils.file.MimeTypeUtils;
import com.xinelu.manage.domain.appointmentorder.AppointmentOrder;
import com.xinelu.manage.domain.goodsOrder.GoodsOrder;
import com.xinelu.manage.domain.orderevaluateinfo.OrderEvaluateInfo;
import com.xinelu.manage.domain.orderevaluatepictureinfo.OrderEvaluatePictureInfo;
import com.xinelu.manage.mapper.appointmentorder.AppointmentOrderMapper;
import com.xinelu.manage.mapper.goodsOrder.GoodsOrderMapper;
import com.xinelu.manage.mapper.orderevaluateinfo.OrderEvaluateInfoMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;

/**
 * 预约服务订单和商品订单评价信息Service业务层处理
 *
 * @author 张恒
 * @date 2022-11-17
 */
@Slf4j
@Service
public class AppOrderEvaluateServiceImpl implements IAppOrderEvaluateService {
    @Resource
    private OrderEvaluateInfoMapper orderEvaluateInfoMapper;
    @Resource
    private AppOrderEvaluateMapper appOrderEvaluateMapper;
    @Resource
    private AppointmentOrderMapper appointmentOrderMapper;
    @Resource
    private GoodsOrderMapper goodsOrderMapper;
    @Resource
    private XinELuConfig xinYiLuConfig;

    /**
     * 新增预约订单评价
     *
     * @param orderEvaluateInfo 评价信息
     * @return AjaxResult
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public AjaxResult insertAppointmentEvaluate(OrderEvaluateInfo orderEvaluateInfo) {
        OrderEvaluateAndPictureDTO orderEvaluate = appOrderEvaluateMapper.selectOrderEvaluateAndPictureByOrderNo(orderEvaluateInfo.getOrderNo());
        if (Objects.nonNull(orderEvaluate)) {
            return AjaxResult.error("当前订单已评价！");
        }
        AppointmentOrder appointmentOrder = appointmentOrderMapper.selectAppointmentOrderById(null, orderEvaluateInfo.getOrderNo());
        if (Objects.isNull(appointmentOrder) || StringUtils.isBlank(appointmentOrder.getOrderStatus()) || !appointmentOrder.getOrderStatus().equals(OrderStatusEnum.COMPLETE.getInfo())) {
            return AjaxResult.error("当前订单未服务完成，不能评价！");
        }
        if (StringUtils.equals(OrderStatusEnum.EVALUATED.getInfo(), appointmentOrder.getOrderStatus())) {
            return AjaxResult.error("当前订单已评价！");
        }
        orderEvaluateInfo.setCreateTime(LocalDateTime.now());
        int insert = orderEvaluateInfoMapper.insertOrderEvaluateInfo(orderEvaluateInfo);
        if (insert <= 0) {
            throw new ServiceException("评价失败，请联系管理员！");
        }
        //修改订单状态
        appointmentOrderMapper.updateAppointmentOrderStatus(OrderStatusEnum.EVALUATED.getInfo(), orderEvaluateInfo.getOrderNo());
        return AjaxResult.success();
    }

    /**
     * 新增商品订单评价
     *
     * @param orderEvaluateAndPictureDTO 评价信息
     * @return AjaxResult
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public AjaxResult insertGoodsOrderEvaluate(OrderEvaluateAndPictureDTO orderEvaluateAndPictureDTO) {
        OrderEvaluateAndPictureDTO orderEvaluate = appOrderEvaluateMapper.selectOrderEvaluateAndPictureByOrderNo(orderEvaluateAndPictureDTO.getOrderNo());
        if (Objects.nonNull(orderEvaluate)) {
            return AjaxResult.error("当前订单已评价！");
        }
        //泉医模块评价判断
        if (OrderSourceEnum.SPRING_DOCTOR.getInfo().equals(orderEvaluateAndPictureDTO.getOrderSource())) {
            GoodsOrder goodsOrder = appOrderEvaluateMapper.selectGoodsOrderByOrderNo(orderEvaluateAndPictureDTO.getOrderNo());
            if (Objects.isNull(goodsOrder)) {
                return AjaxResult.error("当前订单以失效，请选择其他订单信息！");
            }
            if (OrderTypeEnum.HEALTH_CONSULTATION.getInfo().equals(goodsOrder.getOrderType()) && (StringUtils.isBlank(goodsOrder.getOrderStatus()) || !goodsOrder.getOrderStatus().equals(GooodsOrderStatusEnum.COMPLETED.getInfo()))) {
                return AjaxResult.error("当前订单未确认完成，请完成后进行评价！");
            }
            if (!OrderTypeEnum.HEALTH_CONSULTATION.getInfo().equals(goodsOrder.getOrderType()) && (StringUtils.isBlank(goodsOrder.getOrderStatus()) || !goodsOrder.getOrderStatus().equals(GooodsOrderStatusEnum.RECEIVED_GOODS.getInfo()))) {
                return AjaxResult.error("当前订单未确认收货，请先确认收货后进行评价！");
            }
            if (StringUtils.equals(GooodsOrderStatusEnum.EVALUATED.getInfo(), goodsOrder.getOrderStatus())) {
                return AjaxResult.error("当前订单已评价！");
            }
        }
        //新增评价信息
        OrderEvaluateInfo orderEvaluateInfo = new OrderEvaluateInfo();
        BeanUtils.copyProperties(orderEvaluateAndPictureDTO, orderEvaluateInfo);
        orderEvaluateInfo.setCreateTime(LocalDateTime.now());
        int insertOrderEvaluateInfo = orderEvaluateInfoMapper.insertOrderEvaluateInfo(orderEvaluateInfo);
        if (insertOrderEvaluateInfo <= 0) {
            throw new ServiceException("评价失败，请联系管理员！");
        }
        //家医评价终止
        if (OrderSourceEnum.FAMILY_DOCTOR.getInfo().equals(orderEvaluateAndPictureDTO.getOrderSource())) {
            return AjaxResult.success();
        }
        //新增评价图片
        List<OrderEvaluatePictureInfo> afferentEvaluatePicture = orderEvaluateAndPictureDTO.getOrderEvaluatePictureInfoList();
        if (CollectionUtils.isNotEmpty(afferentEvaluatePicture)) {
            afferentEvaluatePicture.forEach(item -> {
                item.setOrderEvaluateId(orderEvaluateInfo.getId());
                item.setCreateTime(LocalDateTime.now());
            });
            int insertPictureList = appOrderEvaluateMapper.insertOrderEvaluatePictureList(afferentEvaluatePicture);
            if (insertPictureList <= 0) {
                throw new ServiceException("评价失败，请联系管理员！");
            }
        }
        //修改订单表状态
        goodsOrderMapper.updateGoodsOrderStatus(GooodsOrderStatusEnum.EVALUATED.getInfo(), orderEvaluateAndPictureDTO.getOrderNo());
        return AjaxResult.success();
    }

    /**
     * 通过订单编号查询评价
     *
     * @param orderNo 订单编号
     * @return OrderEvaluateInfo
     */
    @Override
    public OrderEvaluateAndPictureDTO selectOrderEvaluateAndPicture(String orderNo) {
        return appOrderEvaluateMapper.selectOrderEvaluateAndPictureByOrderNo(orderNo);
    }

    /**
     * 会员App和小程序订单评价
     *
     * @param file    图片文件
     * @param orderNo 点单编号
     * @return 结果
     * @throws Exception 异常信息
     */
    @Override
    public AjaxResult uploadOrderEvaluatePictureUrl(MultipartFile file, String orderNo) throws Exception {
        if (StringUtils.isBlank(file.getOriginalFilename())) {
            return AjaxResult.error("请选择所要上传的评价图片！");
        }
        //获取路径名称
        String uploadPathUrl = XinELuConfig.getProfile() + xinYiLuConfig.getEvaluatePictureUrl() + "/" + orderNo;
        //上传图片
        String pictureName = FileUploadUtils.uploadPictureUrlPath(uploadPathUrl, file, MimeTypeUtils.IMAGE_EXTENSION);
        if (StringUtils.isBlank(pictureName)) {
            throw new ServiceException(pictureName + "订单评价图片上传失败，请联系管理员！");
        }
        //获取返回值
        AjaxResult ajax = AjaxResult.success("上传成功！");
        ajax.put("imgUrl", pictureName);
        return ajax;
    }
}
