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


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.bohuikeji.delivery.backstage.entity.TLogistics;
import com.bohuikeji.delivery.backstage.entity.TLogisticsRecord;
import com.bohuikeji.delivery.backstage.entity.TOrder;
import com.bohuikeji.delivery.backstage.service.ITLogisticsRecordService;
import com.bohuikeji.delivery.backstage.service.ITLogisticsService;
import com.bohuikeji.delivery.backstage.service.ITOrderService;
import com.bohuikeji.frame.module.admin.base.ApiController;
import com.bohuikeji.frame.module.admin.base.ControllerBase;
import com.bohuikeji.frame.module.admin.entity.back.APIEntity;
import com.bohuikeji.frame.module.admin.entity.back.DataPage;
import com.bohuikeji.frame.module.admin.entity.sys.SysError;
import com.bohuikeji.frame.module.admin.utils.Constants;
import com.bohuikeji.frame.module.admin.utils.StringUtils;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 物流（包裹）信息表
 * </p>
 *
 * @author 10552
 * @since 2019-09-10
 */
@Api(tags = {"物流（包裹）信息相关接口"})
@RestController
@RequestMapping("/api/logistics")
@Slf4j
public class TLogisticsApi extends ApiController {

    @Autowired
    ITLogisticsService logisticsService;
    @Autowired
    ITLogisticsRecordService logisticsRecordService;

    @Autowired
    ITOrderService orderService;


    /**
     * 根据订单号查看物流列表（客户/派送员）
     * @return
     */
    @ApiOperation("根据订单号查看物流列表（客户/派送员）")
    @GetMapping("/getLogistics")
    //配置忽略参数
    @ApiOperationSupport(ignoreParameters = {"records","total","searchCount"})
    public APIEntity<IPage<TLogistics>> getLogistics(DataPage<TLogistics> page, String orderNum){
        return APIEntity.success(logisticsService.page(page,new QueryWrapper<TLogistics>().eq("order_num",orderNum)));
    }

    /**
     * 根据订单号查看物流列表（派送员）
     * @return
     */
    @ApiOperation("派送员根据订单号查看包裹")
    @GetMapping("/getLogByCourier")
    //配置忽略参数
    @ApiOperationSupport(ignoreParameters = {"records","total","searchCount"})
    @ApiImplicitParams({
            @ApiImplicitParam(name="orderNum",value="订单号",dataType="String", paramType = "query")})
    public APIEntity<TLogistics> getLogByCourier(String orderNum){
        List<TLogistics> logistics  = logisticsService.list(new QueryWrapper<TLogistics>().eq("order_num",orderNum));
        if(logistics != null && logistics.size() > 0){
            return APIEntity.success(logistics.get(0));
        }
        return APIEntity.fail("该订单没有物流号");
    }

    /**
     * 根据物流id查询物流详情
     * @return
     */
    @ApiOperation("根据物流id查询物流详情")
    @GetMapping("/getLog")
    @ApiOperationSupport(ignoreParameters = {"records","total","searchCount"})
    @ApiImplicitParams({
            @ApiImplicitParam(name="logisticsId",value="物流id",dataType="Integer", paramType = "query")})
    public APIEntity<TLogistics> getLog(Integer logisticsId){
        return APIEntity.success(logisticsService.getOne(new QueryWrapper<TLogistics>().eq("logistics_id",logisticsId)));
    }

    /**
     * 根据物流号查询物流详情
     * @return
     */
    @ApiOperation("根据物流号查询物流详情")
    @GetMapping("/getLogNum")
    @ApiOperationSupport(ignoreParameters = {"records","total","searchCount"})
    @ApiImplicitParams({
            @ApiImplicitParam(name="logisticsNum",value="物流id",dataType="Integer", paramType = "query")})
    public APIEntity<TLogistics> getLogNum(String logisticsNum){
        TLogistics tLogistics = logisticsService.getOne(new QueryWrapper<TLogistics>().eq("logistics_num",logisticsNum));
        return APIEntity.success(tLogistics);
    }

    /**
     * 根据物流号查看物流跟踪状态
     * @return
     */
    @ApiOperation("根据物流号查看物流跟踪状态")
    @GetMapping("/getLogRec")
    @ApiOperationSupport(ignoreParameters = {"records","total","searchCount"})
    @ApiImplicitParams({
            @ApiImplicitParam(name="logisticsNum",value="物流号",dataType="String", paramType = "query",required = true)})
    public APIEntity<List<TLogisticsRecord>> getLogRec(String logisticsNum){
        if(StringUtils.isBlank(logisticsNum)){
            return APIEntity.fail("物流号不能为空");
        }
        return APIEntity.success(logisticsRecordService.list(new QueryWrapper<TLogisticsRecord>().eq("logistics_num",logisticsNum)));
    }

    /**
     * 根据物流id删除包裹信息
     * @return
     */
    @ApiOperation("根据物流id删除包裹信息")
    @GetMapping("/delLogRec")
    @ApiOperationSupport(ignoreParameters = {"records","total","searchCount"})
    @ApiImplicitParams({
            @ApiImplicitParam(name="logisticsId",value="物流id",dataType="String", paramType = "query",required = true)})
    public APIEntity delLogRec(Integer logisticsId){
        TLogistics tLogistics=logisticsService.getById(logisticsId);
        if(tLogistics==null){
            return APIEntity.success("删除成功");
        }
        TOrder order=orderService.getById(tLogistics.getOrderId());
        if(order==null){
            return APIEntity.fail("未找到订单信息，可能订单已被管理员更改，请刷新重试！");
        }
        if("已完成".equals(order.getOrderStatus())){
            return APIEntity.fail("该订单已经完成，无法删除物流信息！");
        }
        if(tLogistics.deleteById()){
            return APIEntity.success("删除成功");
        }
        return APIEntity.fail("系统繁忙，请稍后再试！");
    }
    /**
     * 派送员App添加物流信息
     * @param logistics
     * @return
     */
    @ApiOperation("派送员App修改包裹信息")
    @PostMapping("/updateLog")

    public APIEntity addLog(@RequestBody TLogistics logistics){
        log.info("待更新物流实体：[{}]",logistics);
        if(StringUtils.isBlank(logistics.getLogisticsNum())){
            return APIEntity.fail("物流号不能为空");
        }
        if (logisticsService.updateById(logistics)){
            return APIEntity.success(logistics);
        }
        return APIEntity.fail("更新失败，系统繁忙中~").setStatus(412);
    }

}
