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


import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bohuikeji.delivery.backstage.entity.TCourier;
import com.bohuikeji.delivery.backstage.entity.TOrder;
import com.bohuikeji.delivery.backstage.entity.socket.AdminNotice;
import com.bohuikeji.delivery.backstage.service.*;
import com.bohuikeji.frame.module.admin.base.ApiController;
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.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.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 派送员类型申请切换接口
 */
@Slf4j
@Api(tags = {"派送员类型申请切换接口"})
@RestController
@RequestMapping("/api/type")
public class CourierTypeExamineApi extends ApiController {

    @Autowired
    ITCourierService courierService;
    @Autowired
    ITOrderService orderService;
    @Autowired
    ITLogisticsService logisticsService;
    @Autowired
    ITLogisticsRecordService logisticsRecordService;
    @Autowired
    ISysConfigService sysConfigService;
    @Autowired
    ISysConfigService configService;
    @Autowired
    ITCourierTypeExamineService courierTypeExamineService;
    @Autowired
    ISocketService socketService;



    @ApiOperation("获取骑手类型")
    @GetMapping("/service/courier/gettype")
    //配置忽略参数
    @ApiOperationSupport(ignoreParameters = {"records","total","searchCount"})
    @ApiImplicitParams({
            @ApiImplicitParam(name="groupKey",value="position_type",dataType="String", paramType = "query"),})
    public APIEntity<List<SysConfig>> getType(String groupKey){
        List<SysConfig> sysConfigs=sysConfigService.list(
                new QueryWrapper<SysConfig>().eq("config_group",groupKey));
        return APIEntity.success(sysConfigs);
    }

    @ApiOperation("根据骑手类型id获取数据信息")
    @GetMapping("/service/courier/gettypeinfo")
    //配置忽略参数
    @ApiOperationSupport(ignoreParameters = {"records","total","searchCount"})
    @ApiImplicitParams({
            @ApiImplicitParam(name="configDefault",value="0、1、2",dataType="String", paramType = "query"),})
    public APIEntity<JSONObject> getTypeInfo(String configDefault){
        JSONObject jsonObject = new JSONObject();
        SysConfig sysConfig=sysConfigService.getOne(new QueryWrapper<SysConfig>().eq("config_group","position_type").eq("config_default",Integer.parseInt(configDefault)));
        jsonObject.put("typeId",sysConfig.getConfigId());
        jsonObject.put("typename",sysConfig.getConfigName());
        jsonObject.put("typemark",sysConfig.getConfigMark().replaceAll("。","\r\n"));
        return APIEntity.success(jsonObject);
    }


    /**
     * 派送员类型切换申请
     * @return
     */
    @ApiOperation("派送员类型切换申请")
    @PostMapping("/settypeexamine")
    @ApiOperationSupport(ignoreParameters = {"records","total","searchCount","id"})
    @ApiImplicitParams({
            @ApiImplicitParam(name="configDefault",value="0、1、2",dataType="String", paramType = "query")})
    public APIEntity setTypeExamine(@RequestAttribute("JwtUserId") Integer id,@RequestBody JSONObject jsonObject){
        log.info("!!!!!!!!!!!!!!!!!!!!:[{}]",jsonObject);
        TCourier courier = courierService.getById(id);
        if("审核中".equals(courier.getCourierType())){
            return APIEntity.fail("您好，您已有申请审核的记录，请不要重复提交申请。");
        }
        if("未认证".equals(courier.getCourierAuth())){
            return APIEntity.fail("您好，您的信息未认证，无法切换骑手类型");
        }
        if("审核中".equals(courier.getCourierAuth())){
            return APIEntity.fail("您好，您的信息正在审核中，无法切换骑手类型");
        }
        if("认证失败".equals(courier.getCourierAuth())){
            return APIEntity.fail("您好，您的信息认证失败，无法切换骑手类型");
        }
        if("退款中".equals(courier.getCourierPayStatus())){
            return APIEntity.fail("您好，您有退款中的记录，请退款审核通过后再进行类型切换操作。");
        }
        SysConfig config=sysConfigService.getOne(new QueryWrapper<SysConfig>().eq("config_group","position_type").eq("config_name",jsonObject.getString("configDefault")));
        if(config.getConfigName().equals(courier.getCourierType())){
            return APIEntity.fail("您好，您申请的类型不能与当前类型一致。");
        }
        List<TOrder> orders = orderService.list(new QueryWrapper<TOrder>().eq("courier_id",id).eq("order_status","已完成").eq("courier_sett_status","未结算"));
        if("兼职".equals(courier.getCourierType()) && orders.size()>0 || "营销".equals(courier.getCourierType()) && orders.size()>0){
            return APIEntity.fail("您好，您当前有未结算的订单，请结算完成后再申请切换骑手类型");
        }
        if(courierTypeExamineService.addTypeExamine(courier,config,jsonObject)){
            socketService.sendAdminMsg(new AdminNotice("您好，有新的配送员提交类型切换申请，请及时审核！")
                    .setAudio("https://oss.bhynm.com/delivery/upload/2022/3/15/164731305832261.mp3")
                    .setFranId(courier.getFranId()));
        }
        return APIEntity.success(courier);
    }

}
