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

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bohuikeji.delivery.backstage.entity.TConfig;
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.util.capacity.HllUtils;
import com.bohuikeji.frame.module.admin.entity.TFranchiseeInformation;
import com.bohuikeji.frame.module.admin.entity.back.APIEntity;
import com.bohuikeji.frame.module.admin.entity.sys.SysConfig;
import com.bohuikeji.frame.module.admin.service.ISysConfigService;
import com.bohuikeji.frame.module.admin.service.ITFranchiseeInformationService;
import com.bohuikeji.frame.module.admin.utils.MapUtils;
import com.bohuikeji.frame.module.admin.utils.StringUtils;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@Api(tags = {"货拉拉配置接口类"})
@RestController
@RequestMapping("/api/v2/hulala/config")
@RequiredArgsConstructor
@Slf4j
public class HuolalaConfig {

    final ITConfigService configService;
    final ISysConfigService sysConfigService;
    final ITFranchiseeInformationService informationService;
    final ITCustomerService customerService;
    final ITCustomerTemplateService templateService;
    final ITFranchiseeInformationService franService;

    @ApiOperation("出租拉货计费选择")
    @GetMapping("/lease/goods")
    //配置忽略参数
    @ApiOperationSupport(ignoreParameters = {"records","total","searchCount"})
    @ApiImplicitParams({
            @ApiImplicitParam(name="groupKey",value="carry_type,car_type_lashuo",dataType="String", paramType = "query"),})
    public APIEntity<List<SysConfig>> getLeaseGoods(String groupKey,String location){
        log.info("@@@@@@@@@@@@@@@@@@@:配置组：{}，经纬度：{}",groupKey,location);
        TFranchiseeInformation fran=franService.checkPio(MapUtils.getAddressReGeo(location));
        List<TConfig> configs;
        if (fran==null){
            log.error("【上级加盟商获取异常】参数：key={}，location={}",groupKey,location);
            configs=configService.list(new QueryWrapper<TConfig>().eq("config_group",groupKey).eq("fran_id",1).eq("show_type",1).orderByDesc("config_sort"));
        }
        else{
            configs=configService.list(new QueryWrapper<TConfig>().eq("config_group",groupKey).eq("fran_id",fran.getFranId()).eq("show_type",1).orderByDesc("config_sort"));
        }
        List<SysConfig> sysConfigKey=new ArrayList<>(configs.size());
        configs.forEach(config->{
            sysConfigKey.add(config.toOldConfig());
        });
        return APIEntity.success(sysConfigKey);
    }

}
