package org.jeecg.modules.app.controller.my;


import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import me.zhyd.oauth.utils.UuidUtils;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.app.dto.PageDTO;
import org.jeecg.modules.app.dto.evaluate.AddEvaluateDTO;
import org.jeecg.modules.app.dto.evaluate.FindEvaluateDTO;
import org.jeecg.modules.app.dto.evaluate.FindEvaluatePage;
import org.jeecg.modules.system.app.dto.evaluate.FindEvaluateRequestDTO;
import org.jeecg.modules.system.app.dto.evaluate.FindEvaluateResponseDTO;
import org.jeecg.modules.system.app.entity.AppOrder;
import org.jeecg.modules.system.app.entity.Evaluate;
import org.jeecg.modules.system.app.service.IAppOrderService;
import org.jeecg.modules.system.app.service.IEvaluateService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;


/**
 * 评价
 */
@Slf4j
@Tag(name = "app评价相关接口")
@RestController
@RequestMapping("/app/my/evaluate")
public class AppEvaluateController {

    @Autowired
    private IEvaluateService evaluateService;
    @Autowired
    private IAppOrderService iAppOrderService;


    /**
     * 查询评价列表接口
     *
     * @param pageDTO
     * @return
     */
    @GetMapping("/findByPage")
    @Operation(summary = "app评价分页查询")
    public Result<IPage<FindEvaluateResponseDTO>> findByList(PageDTO pageDTO) {
        FindEvaluateRequestDTO findEvaluateRequestDTO = new FindEvaluateRequestDTO();
        LoginUser principal = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        findEvaluateRequestDTO.setUserId(principal.getId());
//        findEvaluateRequestDTO.setCheckStatus(1);
        return Result.ok(evaluateService.findPage(pageDTO.getPageNo(), pageDTO.getPageSize(), findEvaluateRequestDTO));
    }


    @PostMapping("/submitFeedback")
    @Operation(summary = "提交评价")
    public Result<String> submitFeedback(@RequestBody AddEvaluateDTO addEvaluateDTO) {
        //缺少订单校验逻辑 需要校验订单并且
        AppOrder appOrder = iAppOrderService.getById(addEvaluateDTO.getOrderId());
        if (appOrder==null){
            return Result.error("提交评价失败,未查询到订单");
        }
        Evaluate evaluate =  evaluateService.findByOrder(appOrder.getId());
        if (evaluate!=null) {
            return Result.error("提交评价失败,该订单已经提交");
        }
        evaluate = new Evaluate();
        LoginUser principal = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        BeanUtils.copyProperties(addEvaluateDTO, evaluate);
        evaluate.setId(UuidUtils.getUUID());
        evaluate.setUserId(principal.getId());
        evaluate.setCreateTime(new Date());
        evaluate.setDeptId(appOrder.getTenantId());
        evaluate.setOrgCode(appOrder.getOrgCode());
        boolean save = evaluateService.save(evaluate);
        if (!save) {
            return Result.error("提交评价失败,请联系管理员");
        }
        return Result.ok("提交成功");
    }
    @GetMapping("/findByOrderPage")
    @Operation(summary = "app订单相关评价分页查询")
    public Result<FindEvaluatePage<FindEvaluateResponseDTO>> findByOrderPage(FindEvaluateDTO findEvaluateDTO) {
        return Result.ok(evaluateService.findByOrderPage(findEvaluateDTO));
    }

}
