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

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.bohuikeji.delivery.backstage.entity.TConfig;
import com.bohuikeji.delivery.backstage.entity.TCustomerTemplate;
import com.bohuikeji.delivery.backstage.entity.TOpen;
import com.bohuikeji.delivery.backstage.entity.TOrder;
import com.bohuikeji.delivery.backstage.mapper.TOpenMapper;
import com.bohuikeji.delivery.backstage.service.ITConfigService;
import com.bohuikeji.delivery.backstage.service.ITCustomerService;
import com.bohuikeji.delivery.backstage.service.ITCustomerTemplateService;
import com.bohuikeji.delivery.backstage.service.ITOpenService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bohuikeji.frame.module.admin.base.ServiceImplBase;
import com.bohuikeji.frame.module.admin.component.RedisUtil;
import com.bohuikeji.frame.module.admin.entity.sys.SysConfig;
import com.bohuikeji.frame.module.admin.utils.StringUtils;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 开放平台账户 服务实现类
 * </p>
 *
 * @author 10552
 * @since 2021-05-19
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class TOpenServiceImpl extends ServiceImplBase<TOpenMapper, TOpen> implements ITOpenService {
    final RedisUtil redisUtil;
    final ITConfigService configService;
    final ITCustomerTemplateService customerTemplateService;
    final ITCustomerService customerService;
    public static final String OPEN_CACHE_KEY="open:id:";

    @Override
    public TOpen getOpenBySecret(String appKey, String appSecret) {
        return getOne(new QueryWrapper<TOpen>().eq("app_key",appKey).eq("app_secret",appSecret),false);
    }

    @Override
    public boolean isExist(Integer openId) {
        Object id=getObj(new QueryWrapper<TOpen>().select("id").eq("id",openId),o -> {
            return o;
        });
        if (id!=null){
            return true;
        }
        return false;
    }

    /**
     * 使用缓存查询开放平台账户
     *
     * @param id
     * @return
     */
    @Override
    public TOpen getCacheById(Integer id) {
        TOpen open=redisUtil.get(OPEN_CACHE_KEY+id,TOpen.class);
        if (open==null){
            open=getById(id);
            //更新缓存到Redis，时间30分钟
            redisUtil.set(OPEN_CACHE_KEY+id,open,60*30);
        }
        return open;
    }

    /**
     * 开放平台创建订单初始化
     *
     * @param customerId 客户ID
     * @return
     */
    @Override
    public TOrder openOrderInit(Integer customerId,TOrder order) {
        //获取客户计费模板ID
        Integer templateId=customerService.selectColumnById(customerId,"template_id",Integer.class);
        if(StringUtils.isBlank(order.getMailingAddress().getLat())||StringUtils.isBlank(order.getMailingAddress().getLng())){
            order.setMailingAddress(order.getMailingAddress().fillAddress());
        }
        if(StringUtils.isBlank(order.getReceiptAddress().getLat())||StringUtils.isBlank(order.getReceiptAddress().getLng())){
            order.setReceiptAddress(order.getReceiptAddress().fillAddress());
        }
        if(templateId!=null){
            //获取计费客户模板Code
            String templateCode=customerTemplateService.selectColumnById(templateId,"template_code",String.class);
            //设置车辆计费项目
            TCustomerTemplate tmp=customerTemplateService.getOpenConfigByCode(templateCode,"car_type_ddc");
            order.setVehicleConfigId(tmp.getTemplateId());
            order.setVehicleName(tmp.getConfigName());
            //设置物品类型计费项
            tmp=customerTemplateService.getOpenConfigByCode(templateCode,"cargo_type");
            order.setCargoConfigId(tmp.getTemplateId());
            order.setCargoName(tmp.getConfigName());
            //设置运输方式给计费项
            tmp=customerTemplateService.getOpenConfigByCode(templateCode,"service_type");
            order.setWayConfigId(tmp.getTemplateId());
            order.setWayName(tmp.getConfigName());

            //设置计费方式
            tmp=customerTemplateService.getOpenConfigByCode(templateCode,"payment_method");

            //暂时先走平台己寄付
            order.setPaymentConfigId(31);
            order.setPaymentType("寄付");
        }
        else {
            /**
             * 如果没有计费模板则按，当地加盟商默认计费规则进行计费。
             */
            String location=order.getMailingAddress().getLocation();
            //设置物品计费项目
            if(StringUtils.isBlank(order.getCargoName())){
                order.setCargoName("生活用品");
            }
            TConfig cargoConfig=configService.getOpenConfigByGroup(order.getCargoName(),"cargo_type",location,customerId);
            order.setCargoConfigId(cargoConfig.getId());
            order.setCargoName(cargoConfig.getConfigName());
            //设置车辆类型计费项
            TConfig vehConfig=configService.getOpenConfigByGroup("电动车","car_type_ddc",location,customerId);
            order.setVehicleConfigId(vehConfig.getId());
            order.setVehicleName(vehConfig.getConfigName());
            //设置服务类型计费项
            TConfig wayConfig=configService.getOpenConfigByGroup("极送-平均1小时","service_type",location,customerId);
            order.setWayConfigId(wayConfig.getId());
            order.setWayName(wayConfig.getConfigName());
            //暂时先走平台己寄付
            order.setPaymentConfigId(31);
            order.setPaymentType("寄付");
        }
        return order;
    }
}
