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

import cn.dev33.satoken.annotation.SaCheckLogin;
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.OrderShipServiceI;
import com.xunhong.erp.turbo.api.biz.dto.cmd.OrderShipCreateCmd;
import com.xunhong.erp.turbo.api.biz.dto.cmd.OrderShipDestroyCmd;
import com.xunhong.erp.turbo.api.biz.dto.cmd.OrderShipGenerateDocumentCmd;
import com.xunhong.erp.turbo.api.biz.dto.cmd.OrderShipUpdateCmd;
import com.xunhong.erp.turbo.api.biz.dto.qry.OrderShipListQry;
import com.xunhong.erp.turbo.api.biz.dto.qry.OrderShipPageQry;
import com.xunhong.erp.turbo.api.biz.dto.qry.OrderShipShowQry;
import com.xunhong.erp.turbo.api.biz.dto.vo.OrderShipVO;
import com.xunhong.erp.turbo.api.rbac.dto.constants.RoleConstant;
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 = "OrderShip", description = "发货单管理")
@RestController("operationOrderShipController")
@RequestMapping(value = "/operation")
@RequiredArgsConstructor
public class OrderShipController {

	@DubboReference(version = "1.0.0")
	private final OrderShipServiceI orderShipService;

	@SaCheckLogin
//    @SaCheckPermission(value = {PermissionConstant.MDB_BUSINESS_SHIP_ORDER_VIEW})
	@GetMapping("listOrderShip")
	@Operation(summary = "发货单列表", method = "GET")
	public MultiResponse<OrderShipVO> listOrderShip(@ModelAttribute @Validated OrderShipListQry orderShipListQry) {
		return MultiResponse.of(orderShipService.list(orderShipListQry));
	}

	@SaCheckLogin
//    @SaCheckPermission(value = {PermissionConstant.MDB_BUSINESS_SHIP_ORDER_CREATE})
	@PostMapping("createOrderShip")
	@Operation(summary = "创建发货单", method = "POST")
	public SingleResponse<OrderShipVO> createOrderShip(@RequestBody @Validated OrderShipCreateCmd orderShipCreateCmd) {
		return SingleResponse.of(orderShipService.create(orderShipCreateCmd));
	}

	@SaCheckLogin
//    @SaCheckPermission(value = {PermissionConstant.MDB_BUSINESS_SHIP_ORDER_VIEW})
	@GetMapping("showOrderShip")
	@Operation(summary = "发货单详情", method = "GET")
	public SingleResponse<OrderShipVO> showOrderShip(@ModelAttribute @Validated OrderShipShowQry orderShipShowQry) {
		return SingleResponse.of(orderShipService.show(orderShipShowQry));
	}

	@SaCheckLogin
//    @SaCheckPermission(value = {PermissionConstant.MDB_BUSINESS_SHIP_ORDER_VIEW})
	@GetMapping("pageOrderShip")
	@Operation(summary = "发货单列表", method = "GET")
	public PageResponse<OrderShipVO> pageOrderShip(@ModelAttribute @Validated OrderShipPageQry orderShipPageQry) {
		String roleSlug = UserSession.USER_THREAD_LOCAL.get().getRoleSlug();
		if (roleSlug.equals(RoleConstant.SUPERADMIN)) {
			orderShipPageQry.setCreatedBy(null);
		}
		PageDTO<OrderShipVO> page = orderShipService.page(orderShipPageQry);
		return PageResponse.of(page.getRecords(), (int) page.getTotal(), (int) page.getSize(), (int) page.getCurrent());
	}

	@SaCheckLogin
//    @SaCheckPermission(value = {PermissionConstant.MDB_BUSINESS_SHIP_ORDER_UPDATE})
	@RequestMapping(value = "updateOrderShip", method = {RequestMethod.PATCH, RequestMethod.PUT})
	@Operation(summary = "发货单更新", method = "PATCH")
	public SingleResponse<OrderShipVO> updateOrderShip(@RequestBody @Validated OrderShipUpdateCmd orderShipUpdateCmd) {
		return SingleResponse.of(orderShipService.update(orderShipUpdateCmd));
	}

	@SaCheckLogin
//    @SaCheckPermission(value = {PermissionConstant.MDB_BUSINESS_SHIP_ORDER_UPDATE})
	@RequestMapping(value = "generateDocumentOrderShip", method = {RequestMethod.PATCH, RequestMethod.PUT})
	@Operation(summary = "发货单生成单据", method = "PATCH")
	public Response generateDocumentOrderShip(@RequestBody @Validated OrderShipGenerateDocumentCmd orderShipGenerateDocumentCmd) {
		orderShipService.generateDocument(orderShipGenerateDocumentCmd);
		return Response.buildSuccess();
	}

	@SaCheckLogin
//    @SaCheckPermission(value = {PermissionConstant.MDB_BUSINESS_SHIP_ORDER_DELETE})
	@DeleteMapping("destroyOrderShip")
	@Operation(summary = "发货单删除", method = "DELETE")
	public Response destroyOrderShip(@RequestBody @Validated OrderShipDestroyCmd orderShipDestroyCmd) {
		orderShipService.destroy(orderShipDestroyCmd);
		return Response.buildSuccess();
	}
}
