package com.pj.tb_orders;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.pj.project4sp.SP;
import com.pj.tb_goods_demand.vo.GoodsDemandVo;
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.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;


/**
 * Controller: tb_orders -- 订单表
 * @author qzy
 */
@RestController
@RequestMapping("/app/TbOrders/")
public class TbOrdersApiController {

	/** 底层 Service 对象 */
	@Autowired
	TbOrdersService tbOrdersService;
	@Autowired
	private MethodOrdersService methodOrdersService;

	/** 增 */
	@RequestMapping("add")
	public AjaxJson add(GoodsDemandVo t){
		return AjaxJson.toAjax(tbOrdersService.addByDetail(t));
	}

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

	/** 删 - 根据id列表 */
	@RequestMapping("deleteByIds")
	public AjaxJson deleteByIds(){
		List<Long> ids = SoMap.getRequestSoMap().getListByComma("ids", long.class);
		int line = SP.publicMapper.deleteByIds(TbOrders.TABLE_NAME, ids);
		return AjaxJson.getByLine(line);
	}

	/** 改 收购商购买*/
	@RequestMapping("purchaserBuy")
	public AjaxJson purchaserBuy(){
		SoMap so = SoMap.getRequestSoMap();
		return AjaxJson.toAjax(tbOrdersService.purchaserBuy(so));
	}

	/** 新增 边民缴税以及各服务费*/
	@RequestMapping("payTax")
	public AjaxJson payTax(){
		SoMap so = SoMap.getRequestSoMap();
		return AjaxJson.toAjax(tbOrdersService.payTax(so));
	}

	/** 查集合 - 查二级市场订单(未被购买的订单) */
	@RequestMapping("getLevelTwoList")
	public AjaxJson getLevelTwoList() {
		SoMap so = SoMap.getRequestSoMap();
		List<TbOrders> list = tbOrdersService.getLevelTwoList(so);
		return AjaxJson.getPageData(so.getDataCount(), list);
	}

	/** 二级市场订单 组长/边民角色入口*/
	@RequestMapping("getLeaderOrderList")
	public AjaxJson getLeaderOrderList() {
		SoMap so = SoMap.getRequestSoMap();
		List<TbOrders> list = tbOrdersService.getLeaderOrderList(so.startPage());
		return AjaxJson.getPageData(so.getDataCount(), list);
	}

	/** 二级市场订单 收购商角色入口*/
	@RequestMapping("getPurchaserOrderList")
	public AjaxJson getPurchaserOrderList() {
		SoMap so = SoMap.getRequestSoMap();
		List<TbOrders> list = tbOrdersService.getPurchaserOrderList(so.startPage());
		return AjaxJson.getPageData(so.getDataCount(), list);
	}


	/** 改 - 收购商确认收货 */
	@RequestMapping("toDelivery")
	public AjaxJson toDelivery(TbOrders t){
		return AjaxJson.toAjax(tbOrdersService.updateById(t));
	}

	/** 改 - 删除状态(0=禁用,1=启用) */
	@RequestMapping("updateDeleteStatus")
//	@SaCheckPermission(TbOrders.PERMISSION_CODE_EDIT)
	public AjaxJson updateDeleteStatus(Long id, Integer value){
		int line = SP.publicMapper.updateColumnById(TbOrders.TABLE_NAME, "delete_status", value, id);
		return AjaxJson.getByLine(line);
	}


	/** 增 一级市场已完成订单进行转售时，新增二级市场订单 */
	@RequestMapping("addOrderByResale")
	public AjaxJson  addOrderByResale(TbOrdersDto ordersDto) {
		return AjaxJson.toAjax(tbOrdersService.addOrderByResale(ordersDto));
	}

	/** 查 根据订单Id查订单详情 */
	@RequestMapping("getDetailById")
	public AjaxJson getDetailById() {
		SoMap so = SoMap.getRequestSoMap();
		List<TbOrders> list = tbOrdersService.getDetailById(so);
		return AjaxJson.getSuccessData(list);
	}


}
