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

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckRole;
import cn.dev33.satoken.annotation.SaMode;
import com.alibaba.cola.dto.MultiResponse;
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.OrderServiceI;
import com.xunhong.erp.turbo.api.biz.dto.cmd.*;
import com.xunhong.erp.turbo.api.biz.dto.qry.*;
import com.xunhong.erp.turbo.api.biz.dto.vo.AuditVO;
import com.xunhong.erp.turbo.api.biz.dto.vo.OrderVO;
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 = "Order", description = "采购订单管理")
@RestController("operationOrderController")
@RequestMapping(value = "/operation")
@RequiredArgsConstructor
public class OrderController {

	@DubboReference(version = "1.0.0")
	private final OrderServiceI orderService;

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

	@SaCheckLogin
//    @SaCheckPermission(value = {PermissionConstant.MDB_BUSINESS_PURCHASE_ORDER_VIEW})
	@GetMapping("listOrder")
	@Operation(summary = "采购订单列表", method = "GET")
	public MultiResponse<OrderVO> listOrder(@ModelAttribute @Validated OrderListQry orderListQry) {
		return MultiResponse.of(orderService.list(orderListQry));
	}


	// 获取某个状态的数量
	@SaCheckLogin
	@GetMapping("countOrderByState")
	@Operation(summary = "获取某个状态的数量", method = "GET")
	public SingleResponse<Long> countOrderByState(@ModelAttribute @Validated OrderCountQry orderCountQry) {
		return SingleResponse.of(orderService.countOrderByState(orderCountQry));
	}

	// 分步提交第一步：车辆信息和经销商信息保存
	@SaCheckLogin
//    @SaCheckPermission(value = {PermissionConstant.MDB_BUSINESS_PURCHASE_ORDER_CREATE})
	@PostMapping("saveOrderStep1")
	@Operation(summary = "采购订单第一步：车辆信息和经销商信息保存", method = "POST")
	public SingleResponse<OrderVO> saveOrderStep1(@RequestBody @Validated OrderStep1Cmd orderStep1Cmd) {
		return SingleResponse.of(orderService.saveStep1(orderStep1Cmd));
	}

	// 分步提交第二步：供应商信息保存
	@SaCheckLogin
	@PostMapping("saveOrderStep2")
	@Operation(summary = "采购订单第二步：供应商信息保存", method = "POST")
	public Response saveOrderStep2(@RequestBody @Validated OrderStep2Cmd orderStep2Cmd) {
		orderService.saveStep2(orderStep2Cmd);
		return Response.buildSuccess();
	}

	// 分步提交第三步：人工和辅料等费用信息保存
	@SaCheckLogin
	@PostMapping("saveOrderStep3")
	@Operation(summary = "采购订单第三步：人工和辅料等费用信息保存", method = "POST")
	public Response saveOrderStep3(@RequestBody @Validated OrderStep3Cmd orderStep3Cmd) {
		orderService.saveStep3(orderStep3Cmd);
		return Response.buildSuccess();
	}

	@SaCheckLogin
	@PostMapping("submitReviewOrder")
	@Operation(summary = "采购订单提审（产地/市场采购员提审）", method = "POST")
	public SingleResponse<AuditVO> submitReviewOrder(@RequestBody @Validated OrderSubmitReviewCmd orderSubmitReviewCmd) {
		AuditVO auditVO = orderService.submitReview(orderSubmitReviewCmd);
		return SingleResponse.of(auditVO);
	}

	@SaCheckLogin
	@PostMapping("withdrawReviewOrder")
	@Operation(summary = "采购订单撤回提审（产地/市场采购员撤回提审）", method = "POST")
	public SingleResponse<AuditVO> withdrawReviewOrder(@RequestBody @Validated OrderWithdrawReviewCmd orderWithdrawReviewCmd) {
		AuditVO auditVO = orderService.withdrawReview(orderWithdrawReviewCmd);
		return SingleResponse.of(auditVO);
	}

	@SaCheckLogin
	@PostMapping("approveOrder")
	@Operation(summary = "采购订单审核（审核员审核）", method = "POST")
	public SingleResponse<AuditVO> approveOrder(@RequestBody @Validated OrderApproveCmd orderApproveCmd) {
		AuditVO auditVO = orderService.approve(orderApproveCmd);
		return SingleResponse.of(auditVO);
	}

	@SaCheckLogin
	@PostMapping("rejectApproveOrder")
	@Operation(summary = "采购订单驳回审核（审核员驳回审核）", method = "POST")
	public SingleResponse<AuditVO> rejectApproveOrder(@RequestBody @Validated OrderRejectApproveCmd orderRejectApproveCmd) {
		AuditVO auditVO = orderService.rejectApprove(orderRejectApproveCmd);
		return SingleResponse.of(auditVO);
	}

	@SaCheckLogin
	@PostMapping("finalApproveOrder")
	@Operation(summary = "采购订单审批（老板审批）", method = "POST")
	public SingleResponse<AuditVO> finalApproveOrder(@RequestBody @Validated OrderFinalApproveCmd orderFinalApproveCmd) {
		AuditVO auditVO = orderService.finalApprove(orderFinalApproveCmd);
		return SingleResponse.of(auditVO);
	}

	@SaCheckLogin
	@PostMapping("rejectFinalOrder")
	@Operation(summary = "采购订单驳回审批（老板驳回审批）", method = "POST")
	public SingleResponse<AuditVO> rejectFinalOrder(@RequestBody @Validated OrderRejectFinalCmd orderRejectFinalCmd) {
		AuditVO auditVO = orderService.rejectFinal(orderRejectFinalCmd);
		return SingleResponse.of(auditVO);
	}

	@SaCheckLogin
//    @SaCheckPermission(value = {PermissionConstant.MDB_BUSINESS_PURCHASE_ORDER_VIEW})
	@GetMapping("showOrder")
	@Operation(summary = "采购订单详情", method = "GET")
	public SingleResponse<OrderVO> showOrder(@ModelAttribute @Validated OrderShowQry orderShowQry) {
		return SingleResponse.of(orderService.show(orderShowQry));
	}

	@SaCheckLogin
	@SaCheckRole(value = {RoleConstant.ORIGIN_ENTRY, RoleConstant.MARKET_BUYER, RoleConstant.BOSS, RoleConstant.REVIEWER, RoleConstant.SUPERADMIN}, mode = SaMode.OR)
//    @SaCheckPermission(value = {PermissionConstant.MDB_BUSINESS_PURCHASE_ORDER_VIEW})
	@GetMapping("pageOrder")
	@Operation(summary = "采购订单列表", method = "GET")
	public PageResponse<OrderVO> pageOrder(@ModelAttribute @Validated OrderPageQry orderPageQry) {
		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);
			orderPageQry.setCreatedBy(adminVO.getAdminId());
		}

		PageDTO<OrderVO> page = orderService.page(orderPageQry);
		return PageResponse.of(page.getRecords(), (int) page.getTotal(), (int) page.getSize(), (int) page.getCurrent());
	}

	// 注释掉原有的创建接口
	@Deprecated
	@SaCheckLogin
//    @SaCheckPermission(value = {PermissionConstant.MDB_BUSINESS_PURCHASE_ORDER_CREATE})
	@PostMapping("createOrder")
	@Operation(summary = "创建采购订单（暂存）", method = "POST")
	public SingleResponse<OrderVO> createOrder(@RequestBody @Validated OrderCreateCmd orderCreateCmd) {
		return SingleResponse.of(orderService.create(orderCreateCmd));
	}

	@Deprecated
	@SaCheckLogin
//	@SaCheckPermission(value = {PermissionConstant.MDB_BUSINESS_PURCHASE_ORDER_UPDATE})
	@RequestMapping(value = "updateOrder", method = {RequestMethod.PATCH, RequestMethod.PUT})
	@Operation(summary = "采购订单更新", method = "PATCH")
	public SingleResponse<OrderVO> updateOrder(@RequestBody @Validated OrderUpdateCmd OrderUpdateCmd) {
		return SingleResponse.of(orderService.update(OrderUpdateCmd));
	}

	@SaCheckLogin
//    @SaCheckPermission(value = {PermissionConstant.MDB_BUSINESS_PURCHASE_ORDER_DELETE})
	@DeleteMapping("destroyOrder")
	@Operation(summary = "采购订单删除", method = "DELETE")
	public Response destroyOrder(@RequestBody @Validated OrderDestroyCmd orderDestroyCmd) {
		orderService.destroy(orderDestroyCmd);
		return Response.buildSuccess();
	}

	@SaCheckLogin
	@GetMapping("getLastVehicleNo")
	@Operation(summary = "获取上一车车次号", method = "GET")
	public SingleResponse<String> getLastVehicleNo(@ModelAttribute @Validated LastVehicleNoQry lastVehicleNoQry) {
		return SingleResponse.of(orderService.getLastVehicleNo(lastVehicleNoQry));
	}

}
