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

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bohuikeji.delivery.backstage.entity.TCourierWithdraw;
import com.bohuikeji.delivery.backstage.entity.TDrivingLicence;
import com.bohuikeji.delivery.backstage.entity.TOrderTransfer;
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 io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

@Api(tags = {"转账记录接口"})
@RestController
@RequestMapping("/api/withdraw")
public class TCourierWithdrawApi extends ApiController {
    @Autowired
    ITCourierWithdrawService itCourierWithdrawService;

    @PostMapping("/save")
    @ApiOperation("申请转账")
    public APIEntity save(@RequestBody TCourierWithdraw tCourierWithdraw){
       itCourierWithdrawService.save(tCourierWithdraw);
        return APIEntity.success("操作成功");
    }
    @ApiOperation("查询派送员提现次数")
    @GetMapping("/getWithdrawCount")
    @ApiImplicitParams({
            @ApiImplicitParam(name="courierId",value="派送员登陆id",dataType="Integer", paramType = "query")})
    public APIEntity getWithdrawCount( Integer courierId){
        QueryWrapper<TCourierWithdraw> wrapper = new QueryWrapper<>();
        wrapper.eq("courier_id",courierId).eq("withdraw_state","已提现");
        Integer count = itCourierWithdrawService.count();
        return APIEntity.success(count);
    }
    @ApiOperation("查询派送员累计提现")
    @GetMapping("/getCountAmount")
    @ApiImplicitParams({
            @ApiImplicitParam(name="courierId",value="派送员登陆id",dataType="Integer", paramType = "query")})
    public APIEntity getCountAmount( Integer courierId){
        QueryWrapper<TCourierWithdraw> wrapper = new QueryWrapper<>();
        wrapper.eq("courier_id",courierId).eq("withdraw_state","已提现");
        List<TCourierWithdraw> list = itCourierWithdrawService.list();
        // 计算累计金额
        BigDecimal countAmount = new BigDecimal(0.00);
        for (TCourierWithdraw i : list){
            countAmount.add(i.getRealityAmount());
        }
        return APIEntity.success(countAmount);
    }

    @ApiOperation("审核员查询申请转账列表")
    @GetMapping("/getWithdraw/anOpen")
    @ApiImplicitParams({
            @ApiImplicitParam(name="courierId",value="派送员登陆id",dataType="Integer", paramType = "query")})
    public APIEntity getWithdrawAnOpen(DataPage page){
        QueryWrapper<TCourierWithdraw> wrapper = new QueryWrapper<>();
        wrapper.eq("withdraw_state","未提现");
        return APIEntity.success(itCourierWithdrawService.page(page,wrapper));
    }

    @ApiOperation("审核员批量批准转账信息")
    @PostMapping("/Withdraw/open")
    public APIEntity getWithdrawAnOpen(@RequestBody List<Integer> ids){
        Collection<TCourierWithdraw> list = itCourierWithdrawService.listByIds(ids);
        for(TCourierWithdraw i : list){
            i.setWithdrawState("已提现");
        }
        itCourierWithdrawService.updateBatchById(list);
        return APIEntity.success();
    }


    @ApiOperation("根据派送员查询转账记录")
    @GetMapping("/getWithdraw")
    @ApiImplicitParams({
            @ApiImplicitParam(name="courierId",value="派送员登陆id",dataType="Integer", paramType = "query")})
    public APIEntity getWithdraw(DataPage page, Integer courierId){
        QueryWrapper<TCourierWithdraw> wrapper = new QueryWrapper<>();
        wrapper.eq("courier_id",courierId);
        return APIEntity.success(itCourierWithdrawService.page(page,wrapper));
    }
}
