package com.xunhong.erp.turbo.admin.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import com.alibaba.cola.dto.PageResponse;
import com.xunhong.erp.turbo.api.biz.api.OrderRebateServiceI;
import com.xunhong.erp.turbo.api.biz.dto.qry.OrderRebatePageQry;
import com.xunhong.erp.turbo.api.biz.dto.vo.OrderRebateVO;
import com.xunhong.erp.turbo.api.rbac.dto.constants.RoleConstant;
import com.xunhong.erp.turbo.api.user.api.AdminServiceI;
import com.xunhong.erp.turbo.api.user.dto.qry.AdminShowQry;
import com.xunhong.erp.turbo.api.user.dto.vo.AdminVO;
import com.xunhong.erp.turbo.base.dto.PageDTO;
import com.xunhong.erp.turbo.base.dto.UserSession;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author shenyifei
 */
@Tag(name = "OrderRebate", description = "订单返点管理")
@RestController("operationOrderRebateController")
@RequestMapping(value = "/operation")
@RequiredArgsConstructor
public class OrderRebateController {

	@DubboReference(version = "1.0.0")
	private final OrderRebateServiceI orderRebateService;

	@DubboReference(version = "1.0.0")
	private final AdminServiceI adminService;


	@SaCheckLogin
//    @SaCheckPermission(value = {PermissionConstant.MDB_BUSINESS_ORDER_SUPPLIER_VIEW})
	@GetMapping("pageOrderRebate")
	@Operation(summary = "订单返点列表", method = "GET")
	public PageResponse<OrderRebateVO> pageOrderRebate(@ModelAttribute @Validated OrderRebatePageQry orderRebatePageQry) {
		String roleSlug = UserSession.USER_THREAD_LOCAL.get().getRoleSlug();
		if (roleSlug.equals(RoleConstant.ORIGIN_ENTRY) || roleSlug.equals(RoleConstant.MARKET_BUYER)) {
			Long userId = UserSession.USER_THREAD_LOCAL.get().getUserId();
			AdminShowQry adminShowQry = new AdminShowQry();
			adminShowQry.setUserId(userId);
			AdminVO adminVO = adminService.show(adminShowQry);
			orderRebatePageQry.setCreatedBy(adminVO.getAdminId());
		}
		PageDTO<OrderRebateVO> page = orderRebateService.page(orderRebatePageQry);
		return PageResponse.of(page.getRecords(), (int) page.getTotal(), (int) page.getSize(), (int) page.getCurrent());
	}
}
