package com.pine.admin.modules.business.controller;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

import com.pine.admin.modules.business.entity.CarOrder;
import com.pine.common.dto.Page;
import com.pine.admin.modules.base.binder.BaseController;
import com.pine.common.dto.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.pine.admin.modules.business.service.CarOrderService;


/**
 * 选配车订单表
 *
 * @author Pine
 * @email 771190883@qq.com
 * @date 2019-07-22 21:11:49
 */
@Api(value = "选配车订单表接口", tags = {"选配车订单表接口"})
@RestController
@RequestMapping("business/CarOrder")
public class CarOrderController extends BaseController {

    @Autowired
    private CarOrderService carOrderService;


    /**
     * <p>获取全部记录。</p>
     */
    @ApiOperation(value = "获取所有选配车订单表数据", notes = "可以根据条件获取")
    @RequestMapping(value = "/all", method = RequestMethod.GET)
    public Result all(CarOrder record) {

        List<CarOrder> pp = carOrderService.getAllList(record);
        return Result.success(true, pp);
    }


    /**
     * <p>根据Id。</p>
     */
    @ApiOperation(value = "获取选配车订单表数据", notes = "根据id来获取选配车订单表详细信息")
    @RequestMapping(value = "/get/{id}", method = RequestMethod.GET)
    public Result getCarOrderEntity(@PathVariable("id") String id) {

        CarOrder data = carOrderService.getById(id);
        return Result.success(true, data);
    }

    /**
     * <p>根据条件获取。</p>
     */
    @ApiOperation(value = "获取选配车订单表数据", notes = "根据条件获取")
    @RequestMapping(value = "/getOne", method = RequestMethod.GET)
    public Result getOne(CarOrder record) {

        CarOrder data = carOrderService.getOne(record);
        return Result.success(true, data);
    }

    /**
     * <p>分页查询。</p>
     */
    @ApiOperation(value = "分页获取选配车订单表数据", notes = "根据条件获取")
    @RequestMapping(value = "/page", method = RequestMethod.GET)
    public Result page(Page page, CarOrder record) {

        Map<String, Object> result = new HashMap<>();

        List<CarOrder> pp = carOrderService.getByPage(page, record);

        result.put("page", page);
        result.put("pp", pp);
        return Result.success(true, result);
    }

    /**
     * <p>退订订单分页查询。</p>
     */
    @ApiOperation(value = "分页获取选配车订单表数据", notes = "根据条件获取")
    @RequestMapping(value = "/backPage", method = RequestMethod.GET)
    public Result backPage(Page page, CarOrder record) {

        Map<String, Object> result = new HashMap<>();

        List<CarOrder> pp = carOrderService.getBackByPage(page, record);

        result.put("page", page);
        result.put("pp", pp);
        return Result.success(true, result);
    }

    /**
     * <p>保存。</p>
     */
    @ApiOperation(value = "新增一条选配车订单表数据", notes = "新增一条选配车订单表数据")
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public Result save(CarOrder record) {

        boolean num = carOrderService.create(record);
        if (num) {
            return Result.success(true, record.getId());
        }
        return Result.error("保存异常");
    }

    /**
     * <p>更新信息。</p>
     */
    @ApiOperation(value = "更新一条选配车订单表数据", notes = "更新一条选配车订单表数据")
    @RequestMapping(value = "/update", method = RequestMethod.PUT)
    public Result updateCarOrder(CarOrder record) {

        boolean num = carOrderService.update(record);
        if (num) {
            return Result.success(true, "保存成功");
        }
        return Result.error("保存异常");
    }

    /**
     * <p>删除。</p>
     */
    @RequestMapping(value = "/del/{id}", method = RequestMethod.DELETE)
    @ApiOperation(value = "删除一条选配车订单表数据", notes = "根据Id一条选配车订单表数据")
    public Result deleteCarOrder(@PathVariable("id") String id) {

        boolean num = carOrderService.delete(id);
        if (num) {
            return Result.success(true, "删除成功");
        }
        return Result.error("删除异常");
    }

    /**
     * <p>批量删除。</p>
     */
    @RequestMapping(value = "/deltetByIds", method = RequestMethod.DELETE)
    @ApiOperation(value = "删除多条选配车订单表数据", notes = "删除id的集合多个用英文逗号分隔")
    public Result delCarOrderAll(@RequestParam(required = true, value = "ids") String ids) {

        boolean num = carOrderService.deltetByIds(ids);
        if (num) {
            return Result.success(true, "删除成功");
        }
        return Result.error("删除异常");
    }

    /**
     * 线下审核状态
     */
    @PutMapping(value = "/offlineReview")
    @ApiOperation(value = "线下审核状态", notes = "线下审核状态")
    public Result offlineReview(@RequestParam(required = true, value = "id") Long id, @RequestParam(required = true, value = "loanStatus") String loanStatus, @RequestParam(required = false, value = "downPaymentRate") BigDecimal downPaymentRate) {


        boolean num = carOrderService.offlineReview(id, loanStatus, downPaymentRate);
        if (num) {
            return Result.success(true, "线下审核成功");
        }
        return Result.error("线下审核异常");
    }


    /**
     * 支付,支付
     */
    @PutMapping(value = "/pay")
    @ApiOperation(value = "支付", notes = "支付")
    public Result pay(@RequestParam(required = true, value = "id") Long id, @RequestParam(required = true, value = "invoicePath") String invoicePath) {

        boolean num = carOrderService.pay(id, invoicePath);
        if (num) {
            return Result.success(true, "支付修改成功");
        }
        return Result.error("支付修改异常");
    }

    /**
     * 交车
     */
    @PutMapping(value = "/delivery")
    @ApiOperation(value = "交车", notes = "交车")
    public Result delivery(@RequestParam(required = true, value = "id") Long id, @RequestParam(required = true, value = "invoicePath") String invoicePath) {

        boolean num = carOrderService.delivery(id, invoicePath);
        if (num) {
            return Result.success(true, "交车成功");
        }
        return Result.error("交车异常");
    }

    /**
     * 交车
     */
    @PostMapping(value = "/returnorder")
    @ApiOperation(value = "退单", notes = "退单")
    public Result returnorder(@RequestParam(required = true, value = "orderId") String orderId, @RequestParam(required = true, value = "customerId") String customerId) {
        String refund = carOrderService.refund(orderId, customerId);
        if (refund.equals("0")) {
            return Result.error("不可退款");


        } else {
            return Result.success(true, refund);

        }
    }

}
