package com.bohuikeji.delivery.backstage.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
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.mapper.TEvaluateMapper;
import com.bohuikeji.delivery.backstage.service.ITEvaluateDetailedService;
import com.bohuikeji.delivery.backstage.service.ITEvaluateService;
import com.bohuikeji.delivery.backstage.service.ITLogisticsService;
import com.bohuikeji.delivery.backstage.service.ITOrderService;
import com.bohuikeji.frame.module.admin.base.ServiceImplBase;
import com.bohuikeji.frame.module.admin.entity.sys.SysConfig;
import com.bohuikeji.frame.module.admin.service.ISysConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 评价记录表 服务实现类
 * </p>
 *
 * @author 10552
 * @since 2019-08-22
 */
@Service
public class TEvaluateServiceImpl extends ServiceImplBase<TEvaluateMapper, TEvaluate> implements ITEvaluateService {
    @Autowired
    ITEvaluateService evaluateService;
    @Autowired
    ITOrderService orderService;
    @Autowired
    ISysConfigService sysConfigService;
    @Autowired
    ITEvaluateDetailedService evaluateDetailedService;
    @Autowired
    ITLogisticsService logisticsService;


    /**
     * 客户评价派送员
     * @param orderNum
     * @param customerId
     * @return
     */
    @Override
    public TEvaluate setCustomer(Integer orderId, Integer customerId) {
        TOrder order = orderService.getOne(new QueryWrapper<TOrder>().eq("order_id",orderId).eq("player_id",customerId));
        TEvaluate tEvaluate = new TEvaluate();
        tEvaluate.setOrderNum(order.getOrderNum());
        tEvaluate.setEvaluateType("客户评价派送员");
        tEvaluate.setEvaluatorId(customerId);
        tEvaluate.setOrderId(orderId);
        tEvaluate.setEvaluateTime(LocalDateTime.now());
        tEvaluate.setEvaluatedPersonId(order.getCourierId());
        evaluateService.save(tEvaluate);
        return tEvaluate;
    }
    /**
     * 派送员评价客户
     * @param logisticsNum
     * @param courierId
     * @return
     */
    /*@Override
    public TEvaluate setCourier(String logisticsNum, Integer courierId) {
        TEvaluate tEvaluate = new TEvaluate();
        TLogistics logistics = logisticsService.getOne(new QueryWrapper<TLogistics>().eq("logistics_num",logisticsNum).eq("courier_id",courierId));
        tEvaluate.setOrderNum(logistics.getOrderNum());
        tEvaluate.setEvaluateType("派送员评价客户");
        tEvaluate.setEvaluatorId(courierId);
        tEvaluate.setEvaluateTime(LocalDateTime.now());
        tEvaluate.setEvaluatedPersonId(logistics.getPlayerId());
        return tEvaluate;
    }*/

    /**
     * 计算积分
     * @param evaluateId
     * @param configArray
     * @return
     */
    @Override
    public String setIntegral(Integer evaluateId, Integer serviceNum,Integer speedNum) {
        TEvaluateDetailed evaluateDetailed = new TEvaluateDetailed();
        int a=0;
        int b=0;
        if(serviceNum !=null){
            SysConfig sysConfig = sysConfigService.getOne(new QueryWrapper<SysConfig>().eq("config_name ","服务态度"));
            a = Integer.valueOf(sysConfig.getConfigValue());
            b = serviceNum;
            int c= a*b;
            evaluateDetailed.setEvaluateId(evaluateId);
            evaluateDetailed.setScoringItems(sysConfig.getConfigName());
            evaluateDetailed.setScoringWeight(a);
            evaluateDetailed.setOriginalScore(b);
            evaluateDetailed.setCalculatingScore(c);
            evaluateDetailedService.save(evaluateDetailed);
        }
        if(speedNum !=null){
            SysConfig sysConfig = sysConfigService.getOne(new QueryWrapper<SysConfig>().eq("config_name ","时效速度"));
            a = Integer.valueOf(sysConfig.getConfigValue());
            b = speedNum;
            int c= a*b;
            evaluateDetailed.setEvaluateId(evaluateId);
            evaluateDetailed.setScoringItems(sysConfig.getConfigName());
            evaluateDetailed.setScoringWeight(a);
            evaluateDetailed.setOriginalScore(b);
            evaluateDetailed.setCalculatingScore(c);
            evaluateDetailedService.save(evaluateDetailed);
        }
        List<TEvaluateDetailed> tEvaluateDetaileds = evaluateDetailedService.list(new QueryWrapper<TEvaluateDetailed>().eq("evaluate_id",evaluateId));
        int sum=0;
        for(TEvaluateDetailed evaluateDetailed1 : tEvaluateDetaileds){
            sum+=evaluateDetailed1.getCalculatingScore();
        }
        String su = String.valueOf(sum);
        return su;
    }
}
