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

import cn.dev33.satoken.annotation.SaCheckLogin;
import com.alibaba.cola.dto.PageResponse;
import com.alibaba.cola.dto.SingleResponse;
import com.xunhong.erp.turbo.api.biz.api.OrderCostServiceI;
import com.xunhong.erp.turbo.api.biz.dto.cmd.OrderCostCreateCmd;
import com.xunhong.erp.turbo.api.biz.dto.qry.OrderCostPageQry;
import com.xunhong.erp.turbo.api.biz.dto.vo.OrderCostVO;
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 = "OrderCost", description = "订单成本项管理")
@RestController("operationOrderCostController")
@RequestMapping(value = "/operation")
@RequiredArgsConstructor
public class OrderCostController {

	@DubboReference(version = "1.0.0")
	private final OrderCostServiceI orderCostService;

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


	@SaCheckLogin
//    @SaCheckPermission(value = {PermissionConstant.MDB_BUSINESS_ORDER_SUPPLIER_VIEW})
	@GetMapping("pageOrderCost")
	@Operation(summary = "订单成本项列表", method = "GET")
	public PageResponse<OrderCostVO> pageOrderCost(@ModelAttribute @Validated OrderCostPageQry orderCostPageQry) {
		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);
			orderCostPageQry.setCreatedBy(adminVO.getAdminId());
		}
		PageDTO<OrderCostVO> page = orderCostService.page(orderCostPageQry);
		return PageResponse.of(page.getRecords(), (int) page.getTotal(), (int) page.getSize(), (int) page.getCurrent());
	}
	@SaCheckLogin
//    @SaCheckPermission(value = {PermissionConstant.MDB_BUSINESS_ORDER_SUPPLIER_UPDATE})
	@PostMapping(value = "createOrderCost")
	@Operation(summary = "创建订单成本项", method = "POST")
	public SingleResponse<OrderCostVO> createOrderCost(@RequestBody @Validated OrderCostCreateCmd orderCostCreateCmd) {
		return SingleResponse.of(orderCostService.create(orderCostCreateCmd));
	}
}
