package cn.pluss.platform.controller.jft;

import cn.pluss.platform.annotation.Cert;
import cn.pluss.platform.annotation.ThirdAuth;
import cn.pluss.platform.api.Result;
import cn.pluss.platform.api.ResultGenerator;
import cn.pluss.platform.dto.jft.JftReceiptOrderDTO;
import cn.pluss.platform.entity.JftPaymentInfo;
import cn.pluss.platform.entity.JftReceiptInfo;
import cn.pluss.platform.entity.JftSmallClass;
import cn.pluss.platform.entity.UserApp;
import cn.pluss.platform.jft.JftPaymentInfoService;
import cn.pluss.platform.jft.JftReceiptInfoService;
import cn.pluss.platform.jft.JftReceiptOrderService;
import cn.pluss.platform.jft.JftSmallClassService;
import cn.pluss.platform.userApp.UserAppService;
import cn.pluss.platform.vo.JftReceiptInfoVO;
import cn.pluss.platform.vo.JftReceiptOrderVO;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * TODO
 * 缴费通业务处理入口
 * @author crystal
 * @date 2022/6/16 12:07
 */
@Cert
@ThirdAuth
@RestController
@RequestMapping(value = "/jft")
public class JftController {

    @Resource
    private JftReceiptInfoService jftReceiptInfoService;

    @Resource
    private UserAppService userAppService;

    @Resource
    private JftPaymentInfoService jftPaymentInfoService;

    @Resource
    private JftReceiptOrderService jftReceiptOrderService;

    @Resource
    private JftSmallClassService jftSmallClassService;

    /**
     * 创建收款单接口
     * @param info
     * @return
     */
    @PostMapping(value = "/create/receipt")
    public Result<String> createReceipt(JftReceiptInfo info){
        String url = jftReceiptInfoService.createReceipt(userAppService.checkApiToken(),info);
        return ResultGenerator.genSuccessResult("创建成功",url);
    }

    /**
     * 缴费通收款单分页查询
     * @param condition
     * @return
     */
    @GetMapping(value = "/receipt/page")
    public Result<Page<JftReceiptInfoVO>> receiptPage(Page<JftReceiptInfoVO> page, JftReceiptInfo condition){
        UserApp userApp = userAppService.checkApiToken();
        condition.setUserId(userApp.getUserId());
        page = jftReceiptInfoService.pageInfo(page,condition);
        return ResultGenerator.genSuccessResult("获取成功",page);
    }

    /**
     * 关闭收款单
     * @param info
     * @return
     */
    @PostMapping(value = "/close/receipt")
    public Result<String> closeReceipt(JftReceiptInfo info){
        userAppService.checkApiToken();
        jftReceiptInfoService.closeReceipt(info);
        return ResultGenerator.genSuccessResult("关闭成功",null);
    }

    /**
     * 收款单详情
     * @param info
     * @return
     */
    @RequestMapping(value = "/receipt/detail")
    public Result<JftReceiptInfoVO> receiptDetail(JftReceiptInfo info){
        userAppService.checkApiToken();
        JftReceiptInfoVO vo = jftReceiptInfoService.receiptDetail(info);
        return ResultGenerator.genSuccessResult("查询成功",vo);
    }


    /**
     * 查询付款配置项
     * @return
     */
    @RequestMapping(value = "/payment/list")
    public Result<List<JftPaymentInfo>> paymentList(){
        userAppService.checkApiToken();
        List<JftPaymentInfo> list = jftPaymentInfoService.list();
        return ResultGenerator.genSuccessResult("查询成功",list);
    }

    /**
     * 获取收款单对应付款信息配置项接口
     * @param info
     * @return
     */
    @RequestMapping(value = "/get/receipt")
    public Result<Object> getReceipt(JftReceiptInfo info){
        userAppService.checkApiToken();
        JftReceiptInfoVO vo = jftReceiptInfoService.getReceipt(info);
        return ResultGenerator.genSuccessResult("获取成功",vo);
    }

    /**
     * 创建缴费通订单信息
     * @param order
     * @return
     */
    @GetMapping(value = "/order/page")
    public Result<Page<JftReceiptOrderVO>> orderPage(Page<JftReceiptOrderVO> page, JftReceiptOrderDTO order){
        UserApp userApp = userAppService.checkApiToken();
        order.setUserId(userApp.getUserId());
        page = jftReceiptOrderService.pageData(page,order);
        return ResultGenerator.genSuccessResult("获取成功",page);
    }

    @PostMapping(value = "/create/order")
    public Result<Object> createOrder(@RequestBody JftReceiptOrderDTO entity){
        UserApp userApp = userAppService.checkApiToken();
        entity.setUserId(userApp.getUserId());
        JSONObject result = jftReceiptOrderService.createOrder(entity);
        return ResultGenerator.genSuccessResult("获取成功",result);
    }

    @GetMapping(value = "/class/page")
    public Result<Page<JftSmallClass>> orderPage(Page<JftSmallClass> page, JftSmallClass smallClass){
        userAppService.checkApiToken();
        QueryWrapper<JftSmallClass> queryWrapper = new QueryWrapper<>(smallClass);
        queryWrapper.eq("status",JftSmallClass.DEUAULT_STATUS).last(" order by sort desc , createTime desc ");
        page = jftSmallClassService.page(page,queryWrapper);
        return ResultGenerator.genSuccessResult("获取成功",page);
    }

}
