package com.xinelu.applet.controller.apporderevaluate;


import com.xinelu.applet.dto.apporderevaluate.OrderEvaluateAndPictureDTO;
import com.xinelu.applet.service.apporderevaluate.IAppOrderEvaluateService;
import com.xinelu.common.annotation.MobileRequestAuthorization;
import com.xinelu.common.constant.Constants;
import com.xinelu.common.core.controller.BaseController;
import com.xinelu.common.core.domain.AjaxResult;
import com.xinelu.common.custominterface.Insert;
import com.xinelu.common.exception.ServiceException;
import com.xinelu.manage.domain.orderevaluateinfo.OrderEvaluateInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.Objects;

/**
 * 预约服务订单和商品订单评价信息Controller
 *
 * @author 张恒
 * @date 2022-11-17
 */
@RestController
@RequestMapping("/nurseApp/orderEvaluate")
public class AppOrderEvaluateController extends BaseController {
    @Resource
    private IAppOrderEvaluateService appOrderEvaluateService;

    /**
     * 小程序和App新增预约订单评价
     *
     * @param orderEvaluateInfo 评价信息
     * @return AjaxResult
     */
    @MobileRequestAuthorization
    @PostMapping("/addAppointmentEvaluate")
    public AjaxResult insertAppointmentOrderEvaluate(@Validated(Insert.class) @RequestBody OrderEvaluateInfo orderEvaluateInfo, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            throw new ServiceException(bindingResult.getAllErrors().get(0).getDefaultMessage());
        }
        return appOrderEvaluateService.insertAppointmentEvaluate(orderEvaluateInfo);
    }

    /**
     * 新增商品订单评价
     *
     * @param orderEvaluateAndPictureDTO 评价信息
     * @return AjaxResult
     */
    @MobileRequestAuthorization
    @PostMapping("/insertGoodsEvaluate")
    public AjaxResult insertGoodsOrderEvaluate(@Validated(Insert.class) @RequestBody OrderEvaluateAndPictureDTO orderEvaluateAndPictureDTO, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            throw new ServiceException(bindingResult.getAllErrors().get(0).getDefaultMessage());
        }
        return appOrderEvaluateService.insertGoodsOrderEvaluate(orderEvaluateAndPictureDTO);
    }

    /**
     * 通过订单编号查询评价信息
     *
     * @param orderNo 订单编号
     * @return AjaxResult
     */
    @MobileRequestAuthorization
    @GetMapping("/selectOrderEvaluate")
    public AjaxResult selectOrderEvaluateInfo(String orderNo) {
        if (StringUtils.isBlank(orderNo)) {
            return AjaxResult.error("请选择订单信息！");
        }
        return AjaxResult.success(appOrderEvaluateService.selectOrderEvaluateAndPicture(orderNo));
    }

    /**
     * 会员App和小程序商品订单评价图片上传
     *
     * @param file    图片文件
     * @param orderNo 点单编号
     * @return 上传结果
     */
    @PostMapping("/uploadEvaluatePictureUrl")
    public AjaxResult uploadOrderEvaluatePictureUrl(MultipartFile file, String orderNo) throws Exception {
        if (StringUtils.isBlank(orderNo)) {
            return AjaxResult.error("请选择要评价的订单信息！");
        }
        if (Objects.isNull(file) || StringUtils.isBlank(file.getOriginalFilename())) {
            return AjaxResult.error("当前文件不存在，无法上传！");
        }
        if (file.getOriginalFilename().contains(Constants.EMPTY)) {
            return AjaxResult.error("当前文件名含有空格，请先去除空格在上传！");
        }
        return appOrderEvaluateService.uploadOrderEvaluatePictureUrl(file, orderNo);
    }
}
