package org.jeecg.modules.orderplatform.controller;

import cn.com.ctop.common.module.service.ISysUserService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.entity.SysUser;
import org.jeecg.modules.orderplatform.entity.PlatformOrder;
import org.jeecg.modules.orderplatform.service.IPlatformOrderService;

import org.jeecg.modules.orderplatform.util.DictHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

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

/**
 * 运营下单
 */
@Slf4j
@RestController
@RequestMapping("/platform/order")
public class PlatformOrderCtrl {

	@Autowired
	private IPlatformOrderService platformOrderService;

	@Autowired
	ISysUserService sysUserService;

	@Autowired
	DictHandler dictHandler;
	/**
	  * 查询
	 */
	@GetMapping(value = "/listByCreatorOrDesignLeader")
	public Result<PageInfo<PlatformOrder>> queryPageList(PlatformOrder platformOrder,
														 @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
														 @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
														 HttpServletRequest req) {
		Result<PageInfo<PlatformOrder>> result = new Result<>();
		QueryWrapper<PlatformOrder> queryWrapper = new QueryWrapper<>();
		//发起人精准匹配
		if((null!=platformOrder.getCreateByName())){
			SysUser sysUser=sysUserService.getUserByRealName(platformOrder.getCreateByName());
			if(null!=sysUser){
				platformOrder.setCreateBy(sysUser.getId());
			}
		}
		queryWrapper.setEntity(platformOrder);
		queryWrapper.orderByDesc("update_time");
		PageHelper.startPage(pageNo,pageSize);
		List<PlatformOrder> platformOrderList= platformOrderService.list(queryWrapper);
		result.setSuccess(true);
		result.setResult(new PageInfo<>(dictHandler.dictHandlerOrders(platformOrderList)));
		return result;
	}


	/**
	  * 订单详情页
	 */
	@GetMapping(value = "/queryById")
	public Result<PlatformOrder> queryById(@RequestParam Long id) {
		Result<PlatformOrder> result = new Result<>();
		PlatformOrder platformOrder = platformOrderService.getById(id);
		result.setResult(dictHandler.dictHandlerOrder(platformOrder));
		result.setSuccess(true);
		return result;
	}
}
