package com.jsowell.web.controller.pile;

import com.jsowell.common.annotation.Log;
import com.jsowell.common.core.controller.BaseController;
import com.jsowell.common.core.domain.AjaxResult;
import com.jsowell.common.core.page.TableDataInfo;
import com.jsowell.common.enums.BusinessType;
import com.jsowell.common.util.poi.ExcelUtil;
import com.jsowell.pile.domain.OrderBasicInfo;
import com.jsowell.pile.dto.ManualSettlementDTO;
import com.jsowell.pile.dto.QueryOrderDTO;
import com.jsowell.pile.service.IOrderBasicInfoService;
import com.jsowell.pile.vo.web.OrderListVO;
import com.jsowell.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 订单Controller
 *
 * @author jsowell
 * @date 2022-09-30
 */
@RestController
@RequestMapping("/order")
public class OrderBasicInfoController extends BaseController {
	@Autowired
	private IOrderBasicInfoService orderBasicInfoService;

	@Autowired
	private OrderService orderService;

	/**
	 * 查询订单列表
	 */
	@PreAuthorize("@ss.hasPermi('order:order:list')")
	@GetMapping("/order/list")
	public TableDataInfo list(QueryOrderDTO dto) {
		startPage();
		List<OrderListVO> list = orderBasicInfoService.selectOrderBasicInfoList(dto);
		return getDataTable(list);
	}

	/**
	 * 根据参数查询用电度数和消费金额
	 */
	@PreAuthorize("@ss.hasPermi('order:order:list')")
	@GetMapping("/order/totalData")
	public AjaxResult getOrderTotalData(QueryOrderDTO orderBasicInfo) {
		return AjaxResult.success(orderBasicInfoService.getOrderTotalData(orderBasicInfo));
	}

	/**
	 * 导出订单列表
	 */
	@PreAuthorize("@ss.hasPermi('order:order:export')")
	@Log(title = "订单", businessType = BusinessType.EXPORT)
	@PostMapping("/order/export")
	public void export(HttpServletResponse response, QueryOrderDTO orderBasicInfo) {
		List<OrderListVO> list = orderBasicInfoService.selectOrderBasicInfoList(orderBasicInfo);
		ExcelUtil<OrderListVO> util = new ExcelUtil<OrderListVO>(OrderListVO.class);
		util.exportExcel(response, list, "订单数据");
	}

	/**
	 * 获取订单详细信息 后管订单详情页
	 * http://localhost:8080/order/orderDetail/88000000000001012211161342359448
	 */
	@PreAuthorize("@ss.hasPermi('order:order:query')")
	@GetMapping(value = "/orderDetail/{orderCode}")
	public AjaxResult getInfo(@PathVariable("orderCode") String orderCode) {
		return AjaxResult.success(orderService.queryOrderDetailInfo(orderCode));
	}

	/**
	 * 修改订单 后管调用
	 */
	@PreAuthorize("@ss.hasPermi('order:order:edit')")
	@Log(title = "订单", businessType = BusinessType.UPDATE)
	@PutMapping("/order")
	public AjaxResult edit(@RequestBody OrderBasicInfo orderBasicInfo) {
		return toAjax(orderBasicInfoService.updateOrderBasicInfo(orderBasicInfo));
	}

	/**
	 * 删除订单
	 */
	@PreAuthorize("@ss.hasPermi('order:order:remove')")
	@Log(title = "订单", businessType = BusinessType.DELETE)
	@DeleteMapping("/order/{ids}")
	public AjaxResult remove(@PathVariable Long[] ids) {
		return toAjax(orderBasicInfoService.deleteOrderBasicInfoByIds(ids));
	}


	/**
	 * 人工结算订单
	 * http://localhost:8080/order/manualSettlementOrder
	 */
	@PostMapping("/manualSettlementOrder")
	public AjaxResult manualSettlementOrder(@RequestBody ManualSettlementDTO dto) {
		return toAjax(orderService.manualSettlementOrder(dto));
	}
}
