package com.bohuikeji.delivery.backstage.controller.api.share;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.bohuikeji.delivery.backstage.entity.TCourier;
import com.bohuikeji.delivery.backstage.entity.TEvaluate;
import com.bohuikeji.delivery.backstage.entity.TEvaluateDetailed;
import com.bohuikeji.delivery.backstage.entity.TOrder;
import com.bohuikeji.delivery.backstage.service.ITCourierService;
import com.bohuikeji.delivery.backstage.service.ITEvaluateDetailedService;
import com.bohuikeji.delivery.backstage.service.ITEvaluateService;
import com.bohuikeji.delivery.backstage.service.ITOrderService;
import com.bohuikeji.frame.module.admin.base.ApiController;
import com.bohuikeji.frame.module.admin.entity.back.APIEntity;
import com.bohuikeji.frame.module.admin.entity.back.DataPage;
import com.bohuikeji.frame.module.admin.service.ISysConfigService;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * 评价记录
 */
@Api(tags = {"评价记录接口"})
@RestController
@Slf4j
@RequestMapping("/api/evaluate")
public class EvaluateApi extends ApiController {

    @Autowired
    ITEvaluateService evaluateService;
    @Autowired
    ITOrderService orderService;
    @Autowired
    ISysConfigService sysConfigService;
    @Autowired
    ITEvaluateDetailedService itEvaluateDetailedService;
    @Autowired
    ITCourierService courierService;

    /**
     * 根据评价记录id查询评价记录详情
     * @return
     */
    @ApiOperation("根据评价记录id查询评价记录详情")
    @GetMapping("/getTEvaluate")
    //配置忽略参数
    @ApiOperationSupport(ignoreParameters = {"records","total","searchCount"})
    @ApiImplicitParams({
            @ApiImplicitParam(name="evaluateId",value="评论id",dataType="Integer", paramType = "query")})
    public APIEntity<IPage<TEvaluate>> getTEvaluate(DataPage<TEvaluate> page, Integer evaluateId){

        return APIEntity.success(evaluateService.page(page,new QueryWrapper<TEvaluate>().eq("evaluate_id",evaluateId)));
    }

    /**
     * 派送员查询评价详情
     * @return
     */
    @ApiOperation("派送员查询评价详情")
    @GetMapping("/getTcourierEvalau")
    //配置忽略参数
    @ApiOperationSupport(ignoreParameters = {"records","total","searchCount","id"})
    public APIEntity getTcourierEvalau(DataPage page, @RequestAttribute("JwtUserId") Integer id){
        JSONArray evaluateCour = new JSONArray();
        page = (DataPage) evaluateService.page(page,new QueryWrapper<TEvaluate>().eq("evaluated_person_id",id));
        List<TEvaluate> tEvaluate = page.getRecords();
        for(TEvaluate evaluate : tEvaluate){
            List<TEvaluateDetailed> tEvaluateDetailed = itEvaluateDetailedService.list(new QueryWrapper<TEvaluateDetailed>().eq("evaluate_id",evaluate.getEvaluateId()));
            evaluate.setTEvaluateDetailed(tEvaluateDetailed);
            evaluateCour.add(evaluate);
        }
        page.setRecords(evaluateCour);
        return APIEntity.success(page);
    }

    /**
     * 客户查询评价记录
     * @return
     */
    @ApiOperation("客户查询评价记录")
    @GetMapping("/getCusterEvalau")
    //配置忽略参数
    @ApiOperationSupport(ignoreParameters = {"records","total","searchCount"})
    @ApiImplicitParams({
            @ApiImplicitParam(name="customerId",value="客户登陆id",dataType="Integer", paramType = "query")})
    public APIEntity<IPage<TEvaluate>> getCusterEvalau(DataPage<TEvaluate> page, Integer customerId){
        return APIEntity.success(evaluateService.page(page,new QueryWrapper<TEvaluate>().eq("evaluated_person_id",customerId)));
    }


    /**
     * 客户派送员相互评价接口
     * @param id
     * @param orderId
     * @param serviceNum
     * @param speedNum
     * @return
     */
    @ApiOperation("客户派送员相互评价接口")
    @PostMapping("/record")
    @ApiImplicitParams({
            @ApiImplicitParam(name="orderId",value="订单Id",dataType="Integer", paramType = "query"),
            @ApiImplicitParam(name="serviceNum",value="服务态度",dataType="Integer", paramType = "query"),
            @ApiImplicitParam(name="speedNum",value="时效速度",dataType="Integer", paramType = "query")})
    public APIEntity record(@RequestAttribute("JwtUserId") Integer id,Integer orderId,Integer serviceNum,Integer speedNum){
        TOrder order = orderService.getOne(new QueryWrapper<TOrder>().eq("evaluate_status","待评价").eq("order_id",orderId));
        if(order==null){
            return APIEntity.success("请不要重复评价");
        }
        TEvaluate tEvaluate=null;
        log.info("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
        tEvaluate = evaluateService.setCustomer(orderId,id);
        tEvaluate.setEvaluateScore(evaluateService.setIntegral(tEvaluate.getEvaluateId(),serviceNum,speedNum));
        TCourier tCourier = courierService.getOne(new QueryWrapper<TCourier>().eq("courier_id",tEvaluate.getEvaluatedPersonId()));
        if(evaluateService.updateById(tEvaluate)){ orderService.updateById(order.setEvaluateStatus("已评价"));
        }
        Double a=evaluateService.getObj(new QueryWrapper<TEvaluate>().select("AVG(evaluate_score)").eq("evaluated_person_id",order.getCourierId()),e->{
            return Double.valueOf(e.toString());
        });
        //Integer num = tEvaluate.sql()("SELECT AVG(evaluate_score) FROM t_evaluate");
        log.info("评价分数1:[{}]",a);
        Double summ = a/10.0;
        courierService.updateById(tCourier.setEvaluateScore(summ));
        return APIEntity.success("评价成功");
    }


}
