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

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bohuikeji.delivery.backstage.entity.TCourier;
import com.bohuikeji.delivery.backstage.service.ITCourierService;
import com.bohuikeji.frame.module.admin.service.ITFranchiseeInformationService;
import com.bohuikeji.frame.module.admin.entity.back.APIEntity;
import com.bohuikeji.frame.module.admin.entity.sys.SysUser;
import com.bohuikeji.frame.module.admin.service.ISysUserService;
import com.bohuikeji.frame.module.admin.utils.StringUtils;
import io.swagger.annotations.*;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;

@Api(tags = {"管理端APP接口——派送员相关"})
@RestController
@RequestMapping("/api/admin/courier")
@Slf4j
@RequiredArgsConstructor
public class AdminCourierApi {

    final ITCourierService courierService;
    final ITFranchiseeInformationService franService;
    final ISysUserService userService;


    /*@ApiOperation("搜索派送员(仅搜索认证成功且已缴费的全职派送员)")
    @GetMapping("/search")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "key", value = "搜索关键字[支持工号,姓名、电话]", dataType = "String", paramType = "query", example = "1724861", required = true)})
    public APIEntity searchCourice(String key){
        //SysUser user=userService.getById(id);
        return APIEntity.success(courierService.list(new QueryWrapper<TCourier>()
                .eq("courier_type","全职")
                .eq("courier_status","正常")
                .eq("courier_pay_status","已缴费")
                .eq("courier_auth","认证成功")
                .like("courier_number",key).or()
                .like("courier_name",key).or()
                .like("courier_phone",key).last(" limit 20")));
    }*/
    @ApiOperation("根据ID获取派送员信息")
    @GetMapping("/get/id")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "courierId", value = "派送员ID", dataType = "int", paramType = "query", example = "55", required = true)})
    public APIEntity getCourier(Integer courierId){
        TCourier courier=courierService.getById(courierId);
        if(courier==null){
            return APIEntity.fail("未找到派送员信息！");
        }
        return APIEntity.success(courier);
    }
    @ApiOperation("根据条件筛选派送员（平台：全部的，加盟商：自己的）")
    @GetMapping("/get/list/all")
    public APIEntity getCourierListAll(@ApiIgnore @RequestAttribute("JwtAdminId") Integer id,String key){
        SysUser user=userService.getById(id);
        List<TCourier> courierList = null;
        if(user.getRoleId().equals(1)){
            if(StringUtils.isNotBlank(key)){
                courierList=courierService.list(new QueryWrapper<TCourier>()
                        .eq("courier_status","正常")
                        .eq("courier_auth","认证成功")
                        .apply(" (courier_phone like \""+key+"%\" or courier_name like \""+key+"%\")"));
            }else {
                courierList=courierService.list(new QueryWrapper<TCourier>()
                        .eq("courier_status","正常")
                        .eq("courier_auth","认证成功"));
            }
        }
        if(user.getRoleId().equals(7)){
            if(StringUtils.isNotBlank(key)){
                courierList=courierService.list(new QueryWrapper<TCourier>()
                        .eq("courier_status","正常")
                        .eq("courier_auth","认证成功")
                        .in("fran_id",franService.queryLow(user.getFranId()))
                        .apply(" (courier_phone like \""+key+"%\" or courier_name like \""+key+"%\")"));
            }else {
                courierList=courierService.list(new QueryWrapper<TCourier>()
                        .eq("courier_status","正常")
                        .eq("courier_auth","认证成功")
                        .in("fran_id",franService.queryLow(user.getFranId())));
            }
        }
        return APIEntity.success(courierList);
    }
}
