package com.fs.app.controller;


import cn.hutool.json.JSONUtil;
import com.fs.app.annotation.Login;
import com.fs.app.vo.FsPackageOrderShareVO;
import com.fs.common.annotation.RepeatSubmit;
import com.fs.common.core.domain.R;
import com.fs.common.utils.ParseUtils;
import com.fs.common.utils.StringUtils;
import com.fs.his.domain.*;
import com.fs.his.dto.FsInquiryOrderPatientDTO;
import com.fs.his.dto.FsStoreOrderAddressDTO;
import com.fs.his.param.*;
import com.fs.his.service.*;
import com.fs.his.vo.*;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.*;


@Api("套餐订单接口")
@RestController
@RequestMapping(value="/app/packageOrder")
public class PackageOrderController extends  AppBaseController {

    @Autowired
    private IFsPackageOrderService packageOrderService;


    @Login
    @ApiOperation("创建订单")
    @PostMapping("/create")
    public R create(@Validated @RequestBody FsPackageOrderCreateParam param, HttpServletRequest request){
        param.setUserId(Long.parseLong(getUserId()));
        return packageOrderService.createOrder(param);
    }

    @Login
    @ApiOperation("计算订单")
    @PostMapping("/compute")
    public R compute(@Validated @RequestBody FsPackageOrderComputeParam param, HttpServletRequest request){
        return packageOrderService.computeOrder(param);

    }

    @Login
    @ApiOperation("支付订单")
    @PostMapping("/pay")
    public R pay(HttpServletRequest request, @Validated @RequestBody FsPackageOrderPayParam param)
    {
        param.setUserId(Long.parseLong(getUserId()));
        return packageOrderService.payOrder(param);


    }

    @Login
    @RepeatSubmit
    @ApiOperation("支付")
    @PostMapping("/payment")
    public R payment(HttpServletRequest request, @Validated @RequestBody FsPackageOrderDoPayParam param)
    {
        param.setUserId(Long.parseLong(getUserId()));
        return packageOrderService.payment(param);
    }

    @Login
    @RepeatSubmit
    @ApiOperation("支付宝支付")
    @PostMapping("/aliPayment")
    public R aliPayment(HttpServletRequest request, @Validated @RequestBody FsPackageOrderDoPayParam param)
    {
        param.setUserId(Long.parseLong(getUserId()));
        return packageOrderService.aliPayment(param);
    }

    @Login
    @RepeatSubmit
    @ApiOperation("H5微信支付")
    @PostMapping("/H5WxPayment")
    public R H5WxPayment(HttpServletRequest request, @Validated @RequestBody FsPackageOrderDoPayParam param)
    {
        param.setUserId(Long.parseLong(getUserId()));
        return packageOrderService.H5WxPayment(param);
    }

    @Login
    @RepeatSubmit
    @ApiOperation("支付宝支付")
    @PostMapping("/H5AliPayment")
    public R h5AliPayment(HttpServletRequest request, @Validated @RequestBody FsPackageOrderDoPayParam param)
    {
        param.setUserId(Long.parseLong(getUserId()));
        return packageOrderService.h5AliPayment(param);
    }

    @Login
    @ApiOperation("获取我的订单列表")
    @GetMapping("/getMyPackageOrderList")
    public R getMyPackageOrderList(FsPackageOrderListUParam param, HttpServletRequest request){
        PageHelper.startPage(param.getPageNum(), param.getPageSize());
        param.setUserId(Long.parseLong(getUserId()));
        List<FsPackageOrderListUVO> list=packageOrderService.selectFsPackageOrderListUVO(param);
        PageInfo<FsPackageOrderListUVO> listPageInfo=new PageInfo<>(list);
        return R.ok().put("data",listPageInfo);
    }

    @Login
    @ApiOperation("获取销售订单列表")
    @GetMapping("/getCompanyUserPackageOrderList")
    public R getCompanyUserPackageOrderList(FsPackageOrderListUParam param, HttpServletRequest request){
        PageHelper.startPage(param.getPageNum(), param.getPageSize());
        param.setCompanyUserId(getCompanyUserId());
        List<FsPackageOrderListUVO> list=packageOrderService.selectFsPackageOrderListUVO(param);
        PageInfo<FsPackageOrderListUVO> listPageInfo=new PageInfo<>(list);
        return R.ok().put("data",listPageInfo);
    }
    @Login
    @ApiOperation("获取订单")
    @GetMapping("/getPackageOrderById")
    public R getPackageOrderById(@RequestParam("orderId") Long orderId, HttpServletRequest request){
        FsPackageOrder order=packageOrderService.selectFsPackageOrderByOrderId(orderId);
//        if(!order.getUserId().equals(Long.parseLong(getUserId()))){
//            return R.error("非法操作");
//        }

        if(order!=null&& StringUtils.isNotEmpty(order.getPatientJson())){
            FsPatient dto= JSONUtil.toBean(order.getPatientJson(),FsPatient.class);
            dto.setMobile(ParseUtils.parsePhone(dto.getMobile()));
            dto.setIdCard(ParseUtils.parseIdCard(dto.getIdCard()));
            order.setPatientJson(JSONUtil.toJsonStr(dto));
        }
        if(order!=null&& StringUtils.isNotEmpty(order.getAddressJson())){
            FsStoreOrderAddressDTO dto= JSONUtil.toBean(order.getAddressJson(),FsStoreOrderAddressDTO.class);
            dto.setUserPhone(ParseUtils.parsePhone(dto.getUserPhone()));
            dto.setUserAddress(ParseUtils.parseIdCard(dto.getUserAddress()));
            order.setPatientJson(JSONUtil.toJsonStr(dto));
        }
        return R.ok().put("order",order);
    }

    @Login
    @ApiOperation("获取订单")
    @GetMapping("/getSharePackageOrderById")
    public R getSharePackageOrderById(@RequestParam("orderId") Long orderId, HttpServletRequest request){
        FsPackageOrder order=packageOrderService.selectFsPackageOrderByOrderId(orderId);
        FsPackageOrderShareVO vo=new FsPackageOrderShareVO();
        BeanUtils.copyProperties(order,vo);
        return R.ok().put("order",vo);
    }

    @Login
    @ApiOperation("获取订单")
    @GetMapping("/getCompanyUserPackageOrderById")
    public R getCompanyUserPackageOrderById(@RequestParam("orderId") Long orderId, HttpServletRequest request){
        FsPackageOrder order=packageOrderService.selectFsPackageOrderByOrderId(orderId);
//        if(!order.getCompanyUserId().equals(getCompanyUserId())){
//            return R.error("非法操作");
//        }
        if(order!=null&& StringUtils.isNotEmpty(order.getPatientJson())){
            FsPatient dto= JSONUtil.toBean(order.getPatientJson(),FsPatient.class);
            dto.setMobile(ParseUtils.parsePhone(dto.getMobile()));
            dto.setIdCard(ParseUtils.parseIdCard(dto.getIdCard()));
            order.setPatientJson(JSONUtil.toJsonStr(dto));
        }
        if(order!=null&& StringUtils.isNotEmpty(order.getAddressJson())){
            FsStoreOrderAddressDTO dto= JSONUtil.toBean(order.getAddressJson(),FsStoreOrderAddressDTO.class);
            dto.setUserPhone(ParseUtils.parsePhone(dto.getUserPhone()));
            dto.setUserAddress(ParseUtils.parseIdCard(dto.getUserAddress()));
            order.setPatientJson(JSONUtil.toJsonStr(dto));
        }

        return R.ok().put("order",order);
    }

    @Login
    @ApiOperation("取消订单")
    @PostMapping("/cancel")
    public R cancel(HttpServletRequest request, @Validated @RequestBody FsPackageOrderCancelParam param)
    {
        return packageOrderService.cancel(param);
    }
    @Login
    @ApiOperation("获取订单总数")
    @GetMapping("/getOrderCount")
    public R getOrderCount(){
        Integer count=packageOrderService.selectFsPackageOrderCount(1,Long.parseLong(getUserId()));

        return R.ok().put("count",count);
    }

}
