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.PurchaseOrderServiceI;
import com.xunhong.erp.turbo.api.biz.dto.cmd.*;
import com.xunhong.erp.turbo.api.biz.dto.qry.LastVehicleNoQry;
import com.xunhong.erp.turbo.api.biz.dto.qry.PurchaseOrderCountQry;
import com.xunhong.erp.turbo.api.biz.dto.qry.PurchaseOrderListQry;
import com.xunhong.erp.turbo.api.biz.dto.qry.PurchaseOrderPageQry;
import com.xunhong.erp.turbo.api.biz.dto.qry.PurchaseOrderShowQry;
import com.xunhong.erp.turbo.api.biz.dto.vo.PurchaseOrderVO;
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 = "PurchaseOrder", description = "采购订单管理")
@RestController("operationPurchaseOrderController")
@RequestMapping(value = "/operation")
@RequiredArgsConstructor
public class PurchaseOrderController {

	@DubboReference(version = "1.0.0")
	private final PurchaseOrderServiceI purchaseOrderService;

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

	@SaCheckLogin
//    @SaCheckPermission(value = {PermissionConstant.MDB_BUSINESS_PURCHASE_ORDER_VIEW})
	@GetMapping("listPurchaseOrder")
	@Operation(summary = "采购订单列表", method = "GET")
	public MultiResponse<PurchaseOrderVO> listPurchaseOrder(@ModelAttribute @Validated PurchaseOrderListQry purchaseOrderListQry) {
		return MultiResponse.of(purchaseOrderService.list(purchaseOrderListQry));
	}


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

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

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

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

	@SaCheckLogin
	@PostMapping("submitReviewPurchaseOrder")
	@Operation(summary = "采购订单提审（录入员提审）", method = "POST")
	public Response submitReviewPurchaseOrder(@RequestBody @Validated PurchaseOrderSubmitReviewCmd purchaseOrderSubmitReviewCmd) {
		purchaseOrderService.submitReview(purchaseOrderSubmitReviewCmd);
		return Response.buildSuccess();
	}

	@SaCheckLogin
	@PostMapping("withdrawReviewPurchaseOrder")
	@Operation(summary = "采购订单撤回提审（录入员撤回提审）", method = "POST")
	public Response withdrawReviewPurchaseOrder(@RequestBody @Validated PurchaseOrderWithdrawReviewCmd purchaseOrderWithdrawReviewCmd) {
		purchaseOrderService.withdrawReview(purchaseOrderWithdrawReviewCmd);
		return Response.buildSuccess();
	}

	@SaCheckLogin
	@PostMapping("approvePurchaseOrder")
	@Operation(summary = "采购订单审核（审核员审核）", method = "POST")
	public Response approvePurchaseOrder(@RequestBody @Validated PurchaseOrderApproveCmd purchaseOrderApproveCmd) {
		purchaseOrderService.approve(purchaseOrderApproveCmd);
		return Response.buildSuccess();
	}

	@SaCheckLogin
	@PostMapping("rejectApprovePurchaseOrder")
	@Operation(summary = "采购订单驳回审核（审核员驳回审核）", method = "POST")
	public Response rejectApprovePurchaseOrder(@RequestBody @Validated PurchaseOrderRejectApproveCmd purchaseOrderRejectApproveCmd) {
		purchaseOrderService.rejectApprove(purchaseOrderRejectApproveCmd);
		return Response.buildSuccess();
	}

	@SaCheckLogin
	@PostMapping("finalApprovePurchaseOrder")
	@Operation(summary = "采购订单审批（老板审批）", method = "POST")
	public Response finalApprovePurchaseOrder(@RequestBody @Validated PurchaseOrderFinalApproveCmd purchaseOrderFinalApproveCmd) {

		purchaseOrderService.finalApprove(purchaseOrderFinalApproveCmd);
		return Response.buildSuccess();
	}

	@SaCheckLogin
	@PostMapping("rejectFinalPurchaseOrder")
	@Operation(summary = "采购订单驳回审批（老板驳回审批）", method = "POST")
	public Response rejectFinalPurchaseOrder(@RequestBody @Validated PurchaseOrderRejectFinalCmd purchaseOrderRejectFinalCmd) {
		purchaseOrderService.rejectFinal(purchaseOrderRejectFinalCmd);
		return Response.buildSuccess();
	}

	@SaCheckLogin
//    @SaCheckPermission(value = {PermissionConstant.MDB_BUSINESS_PURCHASE_ORDER_VIEW})
	@GetMapping("showPurchaseOrder")
	@Operation(summary = "采购订单详情", method = "GET")
	public SingleResponse<PurchaseOrderVO> showPurchaseOrder(@ModelAttribute @Validated PurchaseOrderShowQry purchaseOrderShowQry) {
		return SingleResponse.of(purchaseOrderService.show(purchaseOrderShowQry));
	}

	@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("pagePurchaseOrder")
	@Operation(summary = "采购订单列表", method = "GET")
	public PageResponse<PurchaseOrderVO> pagePurchaseOrder(@ModelAttribute @Validated PurchaseOrderPageQry purchaseOrderPageQry) {
		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);
			purchaseOrderPageQry.setCreatedBy(adminVO.getAdminId());
		}

		PageDTO<PurchaseOrderVO> page = purchaseOrderService.page(purchaseOrderPageQry);
		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("createPurchaseOrder")
	@Operation(summary = "创建采购订单（暂存）", method = "POST")
	public SingleResponse<PurchaseOrderVO> createPurchaseOrder(@RequestBody @Validated PurchaseOrderCreateCmd purchaseOrderCreateCmd) {
		return SingleResponse.of(purchaseOrderService.create(purchaseOrderCreateCmd));
	}

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

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

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

}
