package com.pj.tb_order;

import com.pj.api.dto.OrderDto;
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("deleteByIds")
	public AjaxJson deleteByIds(){
		List<Long> ids = SoMap.getRequestSoMap().getListByComma("ids", long.class);
		int line = SP.publicMapper.deleteByIds(TbOrder.TABLE_NAME, ids);
		return AjaxJson.getByLine(line);
	}

	/** 删 */
	@RequestMapping("delete")
	public AjaxJson delete(Long id){
		tbOrderService.delete(id);
		return AjaxJson.getSuccess();
	}

	/** 改 */
	@RequestMapping("update")
	public AjaxJson update(TbOrder t){
		tbOrderService.update(t);
		return AjaxJson.getSuccess();
	}

	/** 查 - 根据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);
	}

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



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

	/** 获取一级市场边民订单的所有确认状态 */
	@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));
	}


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

	/** 查询当天的下单记录 */
	@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;
	}

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

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

}
