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

import com.bohuikeji.delivery.backstage.entity.TCourierSaveDetail;
import com.bohuikeji.delivery.backstage.service.ITCourierCardService;
import com.bohuikeji.delivery.backstage.service.ITCourierSaveDetailService;
import com.bohuikeji.delivery.backstage.service.ITCourierService;
import com.bohuikeji.delivery.backstage.service.ITCourierWithdrawService;
import com.bohuikeji.frame.module.admin.base.ApiController;
import com.bohuikeji.frame.module.admin.entity.back.APIEntity;
import com.bohuikeji.frame.module.admin.entity.back.DataPage;
import com.bohuikeji.frame.module.admin.filter.WebLogFilter;
import com.bohuikeji.frame.module.admin.utils.Constants;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 骑手保险相关接口
 * @author 10552
 */
@Api(tags = {"骑手保险相关接口"})
@RestController
@RequestMapping("/api/courier/save")
@RequiredArgsConstructor
@Slf4j
public class TCourierSaveApi extends ApiController {
    final ITCourierService courierService;
    final ITCourierSaveDetailService saveDetailService;
    final ITCourierCardService cardService;
    final ITCourierWithdrawService withdrawService;

    /**
     * 投保
     * @param id
     * @return
     */
    @ApiOperation("骑手投保")
    @GetMapping("/insure")
    APIEntity insure(@ApiIgnore @RequestAttribute("JwtUserId") Integer id){
        if(!cardService.isBuildAliPay(id)){
            return APIEntity.fail("您尚未绑定支付宝账户，请先完成绑定，以便发放补贴金。");
        }
        if ("全职".equals(courierService.getById(id).getCourierType())){
            return APIEntity.fail("不支持全职骑手投保！");
        }
        if (saveDetailService.isInsure(id)){
            return APIEntity.fail("您今日已投保，请勿重复投保！");
        }
       TCourierSaveDetail saveDetail=saveDetailService.getInsurance(id);
        if (saveDetail!=null&&"等待支付".equals(saveDetail.getStatus())){
            return APIEntity.success(saveDetail.getPayUrl());
        }
        //没有正在处理的保单，则直接投保，生成新保单
        return APIEntity.success(saveDetailService.insure(id));
    }

    /**
     * 查询骑手今日是否已经投保
     * @param id
     * @return
     */
    @ApiOperation(value = "查询骑手今日是否已经投保",notes = "如已投保，返回投保信息<br>如未投保，code返回400")
    @GetMapping("/is/insure")
    APIEntity isInsure(@ApiIgnore @RequestAttribute("JwtUserId") Integer id){
        if(saveDetailService.isInsure(id)){
            return APIEntity.success();
        }
        return APIEntity.fail("您今日还未投保,请在订单完成之前进行投保！");
    }

    /**
     * 查询保单详情
     * @param id
     * @return
     */
    @ApiOperation("查询保单详情")
    @GetMapping("/insure/id")
    APIEntity insureDetail(@ApiParam(value = "保单ID") @RequestParam Integer id){
        TCourierSaveDetail detail=saveDetailService.getById(id);
        if (detail!=null){
            return APIEntity.success(detail);
        }
        return APIEntity.fail("未找到保单信息");
    }

    /**
     * 分页查询我的保单记录
     * @param id
     * @param page
     * @return
     */
    @ApiOperation("分页查询我的保单记录")
    @GetMapping("/insure/list/page")
    APIEntity insurePdf(@ApiIgnore @RequestAttribute("JwtUserId") Integer id,DataPage page){
        return APIEntity.success(saveDetailService.pageInsurance(id,page));
    }
    /**
     * 通知回调
     * @param request
     * @return
     */
    @ApiOperation(value = "通知回调",hidden = true)
    @GetMapping("/notice/url")
    void noticeURL(String cpcNoticeNo, String payAmount, HttpServletRequest request, HttpServletResponse response){
        try {
            response.sendRedirect(Constants.BX_END_H5);
        } catch (IOException e) {
            e.printStackTrace();
        }
        log.info("通知回调进入参数：{}", WebLogFilter.getRequestParams(request));
        /*TCourierSaveDetail saveDetail=saveDetailService.getOne(new QueryWrapper<TCourierSaveDetail>()
                .eq("notice_no",cpcNoticeNo).eq("status","投保成功"),false);*/
        /*if (saveDetail!=null){

        }*/
        /*else{
            //回调依赖处理取消，避免给骑手重复退款
            //saveDetailService.insureSuccess(cpcNoticeNo,payAmount);
            try {
                response.sendRedirect(Constants.BX_END_H5);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }*/
    }


}
