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

import cn.dev33.satoken.annotation.SaCheckLogin;
import com.alibaba.cola.dto.PageResponse;
import com.alibaba.cola.dto.Response;
import com.alibaba.cola.dto.SingleResponse;
import com.xunhong.erp.turbo.api.biz.api.OrderSupplierServiceI;
import com.xunhong.erp.turbo.api.biz.dto.cmd.OrderSupplierBatchInvoiceUploadCmd;
import com.xunhong.erp.turbo.api.biz.dto.cmd.OrderSupplierUpdateCmd;
import com.xunhong.erp.turbo.api.biz.dto.qry.OrderSupplierPageQry;
import com.xunhong.erp.turbo.api.biz.dto.vo.OrderSupplierVO;
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.*;

/**
 * @author shenyifei
 */
@Tag(name = "OrderSupplier", description = "订单供应商管理")
@RestController("operationOrderSupplierController")
@RequestMapping(value = "/operation")
@RequiredArgsConstructor
public class OrderSupplierController {

	@DubboReference(version = "1.0.0")
	private final OrderSupplierServiceI orderSupplierService;

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


	@SaCheckLogin
//    @SaCheckPermission(value = {PermissionConstant.MDB_BUSINESS_ORDER_SUPPLIER_VIEW})
	@GetMapping("pageOrderSupplier")
	@Operation(summary = "订单供应商列表", method = "GET")
	public PageResponse<OrderSupplierVO> pageOrderSupplier(@ModelAttribute @Validated OrderSupplierPageQry orderSupplierPageQry) {
		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);
			orderSupplierPageQry.setCreatedBy(adminVO.getAdminId());
		}
		PageDTO<OrderSupplierVO> page = orderSupplierService.page(orderSupplierPageQry);
		return PageResponse.of(page.getRecords(), (int) page.getTotal(), (int) page.getSize(), (int) page.getCurrent());
	}

	@SaCheckLogin
//    @SaCheckPermission(value = {PermissionConstant.MDB_BUSINESS_ORDER_SUPPLIER_UPDATE})
	@RequestMapping(value = "updateOrderSupplier", method = {RequestMethod.PATCH, RequestMethod.PUT})
	@Operation(summary = "订单供应商更新", method = "PATCH")
	public SingleResponse<OrderSupplierVO> updateOrderSupplier(@RequestBody @Validated OrderSupplierUpdateCmd orderSupplierUpdateCmd) {
		return SingleResponse.of(orderSupplierService.update(orderSupplierUpdateCmd));
	}

	@SaCheckLogin
//    @SaCheckPermission(value = {PermissionConstant.MDB_BUSINESS_ORDER_SUPPLIER_BATCH_INVOICE_UPLOAD})
	@PostMapping("batchUploadInvoice")
	@Operation(summary = "批量上传供应商发票", method = "POST")
	public Response batchUploadInvoice(@RequestBody @Validated OrderSupplierBatchInvoiceUploadCmd batchInvoiceUploadCmd) {
		orderSupplierService.batchUploadInvoice(batchInvoiceUploadCmd);
		return SingleResponse.buildSuccess();
	}
}
