package com.pj.tb_order;

import com.pj.api.dto.OrderDto;
import com.pj.current.satoken.StpAPPUserUtil;
import com.pj.project4sp.SP;
import com.pj.tb_order.vo.OrderVo;
import com.pj.tb_order.vo.PeopleConfirmVo;
import com.pj.utils.sg.AjaxJson;
import com.pj.utils.so.SoMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;


/**
 * Controller: tb_order -- 订单表(App接口)
 * @author loovi
 */
@RestController
@RequestMapping("/app/TbOrder/")
public class TbOrderAppController {

	/** 底层 Service 对象 */
	@Autowired
	TbOrderService tbOrderService;




	/** 查 - 根据id */
	@RequestMapping("getById")
	public AjaxJson getById(Long id){
		TbOrder t = tbOrderService.getById(id);
		return AjaxJson.getSuccessData(t);
	}

	/**
	 * 查集合 - 根据条件（参数为空时代表忽略指定条件）
	 * @return
	 */
	@RequestMapping("getList")
	public AjaxJson getList() {
		SoMap so = SoMap.getRequestSoMap();
		List<TbOrder> list = tbOrderService.getAPPList(so.startPage());
		if(list.size() == 0)return AjaxJson.getSuccess("暂无更多的订单消息!");
		return AjaxJson.getPageData(so.getDataCount(), list);
	}

	/**
	 * 查集合 - 根据条件（参数为空时代表忽略指定条件）
	 * @return
	 */
	@RequestMapping("getListBySaleMainId")
	public AjaxJson getListBySaleMainId() {
		SoMap so = SoMap.getRequestSoMap();
		List<TbOrder> list = tbOrderService.getList(so.startPage());
		if(list.size() == 0)return AjaxJson.getSuccess("暂无更多的订单消息!");
		return AjaxJson.getPageData(so.getDataCount(), list);
	}

	/** 查 转售列表：边民查看已完结订单，包括已转售/未转售 */
	@RequestMapping("getResaleOrders")
	public AjaxJson getResaleOrders() {
		List<TbOrder> resaleOrders = tbOrderService.getResaleOrders();
		return AjaxJson.getPageData(resaleOrders.size(), resaleOrders);
	}

	/**
	 * 组长或者边民取消订单
	 * @param orderId  一级市场订单ID
	 */
	@RequestMapping("cancelOrder")
	public AjaxJson cancelOrder(Long orderId) {
		boolean result = tbOrderService.cancelOrder(orderId);
		if(result) return AjaxJson.getSuccess();
		return AjaxJson.getError();
	}

	/**
	 * 改    商铺确认情况[0=待确认,1=已确认,2=拒绝]
	 */
	@PostMapping("updateEnterpriseConfirm")
	public AjaxJson updateEnterpriseConfirm(TbOrder order) {

		return AjaxJson.toAjax(tbOrderService.updateEnterpriseConfirm(order));
	}

	/**
	 * 根据ID获取订单详情
	 * @param orderId
	 * @return
	 */
	@RequestMapping("orderDetail")
	public AjaxJson orderDetail(@RequestParam("orderId")Long orderId){
		return AjaxJson.getSuccessData(tbOrderService.orderDetail(orderId));
	}

	/**
	 * 根据ID获取订单详情
	 * @param orderId
	 * @return
	 */
	@GetMapping("selectPeopleConfirmByOrderId")
	public AjaxJson selectPeopleConfirmByGroupId(Long orderId){
		return AjaxJson.getSuccessData(tbOrderService.selectPeopleConfirmByOrderId(orderId));
	}

	@RequestMapping("getPeopleState")
	public AjaxJson getPeopleState() {
		SoMap so = SoMap.getRequestSoMap();
		List<PeopleConfirmVo> list = tbOrderService.getPeopleState(so);
		return AjaxJson.getPageData(so.getDataCount(), list);
	}



	/** app首页获取边民未确认订单 */
	@RequestMapping("getNoConfirmOrder")
	public AjaxJson getNoConfirmOrder(@RequestParam("cooper_entrust") int cooper_entrust) {
		return AjaxJson.getSuccessData(tbOrderService.getByDeclareType(cooper_entrust));
	}

	/** 获取一级市场边民订单的所有确认状态 */
	@RequestMapping("getConfirmStatus")
	public AjaxJson getConfirmStatus(@RequestParam("orderId")Long orderId) {
		return AjaxJson.getSuccessData(tbOrderService.getConfirmStatus(orderId));
	}

	/** 边民订单确认 */
	@RequestMapping("confirmOrder")
	public AjaxJson confirmOrder(@RequestParam("orderId")Long orderId) {
		return AjaxJson.toAjax(tbOrderService.confirmOrder(orderId));
	}
	/** 边民进口申报确认 */
	@RequestMapping("applyOrder")
	public AjaxJson applyOrder(@RequestParam("orderId")Long orderId) {
		return AjaxJson.toAjax(tbOrderService.applyOrder(orderId));
	}
	/** 互助委托申报确认 */
	@RequestMapping("cooperOrder")
	public AjaxJson cooperOrder(@RequestParam("orderId")Long orderId) {
		return AjaxJson.toAjax(tbOrderService.cooperOrder(orderId));
	}

	@RequestMapping("up")
	public AjaxJson up(@RequestParam("id")Long id,@RequestParam("upPrice")String upPrice) {
		tbOrderService.up(id,upPrice);
		return AjaxJson.getSuccess();
	}

	@RequestMapping("down")
	public AjaxJson down(@RequestParam("id")Long id) {
		tbOrderService.down(id);
		return AjaxJson.getSuccess();
	}


	@RequestMapping("getSaleList")
	public AjaxJson getSaleList(@RequestParam String saleMainId) {
		if (!StpAPPUserUtil.isLogin()){
			return AjaxJson.getSuccess("请登录!");
		}
		SoMap so = SoMap.getRequestSoMap();
		so.put("saleMainId",saleMainId);
		List<TbOrder> list = tbOrderService.getList(so.startPage());
		if(list.size() == 0)return AjaxJson.getSuccess("暂无更多的订单消息!");
		return AjaxJson.getPageData(so.getDataCount(), list);
	}
	@RequestMapping("getOnSaleList")
	public AjaxJson getOnSaleList() {
		if (!StpAPPUserUtil.isLogin()){
			return AjaxJson.getSuccess("请登录!");
		}
		SoMap so = SoMap.getRequestSoMap();
		List<TbOrder> list = tbOrderService.getOnSaleList(so.startPage());
		if(list.size() == 0)return AjaxJson.getSuccess("暂无更多的订单消息!");
		return AjaxJson.getPageData(so.getDataCount(), list);
	}


	@RequestMapping("getPeopleOrderList")
	public AjaxJson getPeopleOrderList() {
		SoMap so = SoMap.getRequestSoMap();
		List<OrderVo> list = tbOrderService.getPeopleOrderList(so.startPage());
		if(list.size() == 0)return AjaxJson.getSuccess("暂无更多的订单消息!");
		return AjaxJson.getPageData(so.getDataCount(), list);
	}


	@PostMapping("orderRefund")
    public AjaxJson orderRefund(@RequestParam("id")Long id) {
		if (!StpAPPUserUtil.isLogin()){
			return AjaxJson.getSuccess("请登录!");
		}
        return AjaxJson.toAjax(tbOrderService.orderRefund(id));
    }

	/*-----           正常业务逻辑👆👆    rpc远程调用👇👇 -----*/

	/** 边民订单确认-收到银行扣款回执 */
	@PostMapping("rpc/confirmOrderFromBank")
	public boolean confirmOrderFromBank(@RequestParam("tradeNo")String tradeNo,@RequestParam("note")String note, @RequestParam("SXB010Status")Integer SXB010Status) {
		boolean result = tbOrderService.confirmOrderFromBank(tradeNo,note, SXB010Status);
		return result;
	}

	/** 收到（一级市场-通车批信息）订单打款回执 */
	@PostMapping("rpc/orderFinishFromBank")
	public boolean orderFinishFromBank(@RequestParam("tradeNo")String tradeNo, @RequestParam("SXB023Status")Integer SXB023Status) {
		boolean result = tbOrderService.orderFinishFromBank(tradeNo, SXB023Status);
		return result;
	}

	/** 收到银行退款回执 */
	@PostMapping("rpc/chargebackFromBank")
	public boolean chargebackFromBank(@RequestParam("tradeNo")String tradeNo, @RequestParam("SXB023Status")Integer SXB010Status, @RequestParam("note")String note) {
		boolean result = tbOrderService.chargebackFromBank(tradeNo, SXB010Status, note);
		return result;
	}

	/** 查询当天的下单记录 */
	@RequestMapping("rpc/selectOrderList")
	public List<OrderDto> selectOrderList( @RequestParam("appUserId") Long appUserId) {
		List<OrderDto> orderDtos = tbOrderService.selectOrderList(appUserId);
		return orderDtos;
	}
	/** 查 - 根据id查orderDto */
	@RequestMapping("rpc/getOrderDtoById")
	public OrderDto getOrderDtoById(@RequestParam("orderId") Long orderId){
		OrderDto t = tbOrderService.getOrderDtoById(orderId);
		return t;
	}

	/** 远程调用  修改订单的叫车状态 */
	@PostMapping("rpc/updateOrderCallCarStatus")
	public boolean updateOrderCallCarStatus(@RequestBody OrderDto orderDto){
		boolean b = tbOrderService.updateOrderCallCarStatus(orderDto);
		return b;
	}

	/** 改 转售状态：根据订单Id把订单改为已转售 */
	@PostMapping("rpc/updateResaleStatus")
	public boolean updateResaleStatus(@RequestParam("id")Long orderId) {
		return tbOrderService.updateResaleStatus(orderId);
	}

	/** 远程调用 根据订单Id修改状态：在二级市场被下单[0=未被下单,1=已被下单] */
	@PostMapping("rpc/updateBeingOrder")
	public boolean updateBeingOrder(@RequestParam("id")Long levelOneOrderId) {
		return tbOrderService.updateBeingOrder(levelOneOrderId);
	}
}
