package com.bonus.zlpt.order.controller;

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

import com.bonus.zlpt.common.core.domain.order.vo.OrderInfoVo;
import com.bonus.zlpt.common.core.utils.poi.ExcelUtil;
import com.bonus.zlpt.common.core.web.controller.BaseController;
import com.bonus.zlpt.common.core.web.domain.AjaxResult;
import com.bonus.zlpt.common.core.web.page.TableDataInfo;
import com.bonus.zlpt.common.log.annotation.Log;
import com.bonus.zlpt.common.log.enums.BusinessType;
import com.bonus.zlpt.common.security.annotation.RequiresPermissions;
import com.bonus.zlpt.order.feign.EquipClient;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.bonus.zlpt.common.core.domain.order.OrderInfo;
import com.bonus.zlpt.order.service.IOrderInfoService;

import static com.bonus.zlpt.common.core.web.domain.AjaxResult.DATA_TAG;

/**
 * 订单信息Controller
 * 
 * @author xsheng
 * @date 2023-12-01
 */
@Api(tags = "订单收据接口")
@RestController
@RequestMapping("/info")
public class OrderInfoController extends BaseController
{
    private final String REJECT_ORDER_STATUS = "39";

    @Autowired
    private IOrderInfoService orderInfoService;
    @Autowired
    EquipClient equipClient;

    /**
     * 查询订单信息列表
     */
    @ApiOperation(value = "订单列表")
    @RequiresPermissions("order:info:list")
    @GetMapping("/list")
    public TableDataInfo list(OrderInfoVo orderInfo)
    {
        startPage();
        List<OrderInfoVo> list = orderInfoService.selectOrderInfoList(orderInfo);
        if (!CollectionUtils.isEmpty(list)) {
            for (OrderInfoVo orderInfoVo : list) {
                Map map1 = (Map) equipClient.getInfo(orderInfoVo.getMaId());
                if (map1 != null) {
                    Map map2 = (Map) map1.get(DATA_TAG);
                    if (map2 != null) {
                        String typeName = (String) map2.get("typeName");
                        String groupName = (String) map2.get("groupName");
                        String modelName = (String) map2.get("modelName");
                        String deviceName = (String) map2.get("deviceName");
                        orderInfoVo.setTypeName(typeName);
                        orderInfoVo.setGroupName(groupName);
                        orderInfoVo.setDeviceName(modelName + deviceName);
                    }
                }
            }
        }
        return getDataTable(list);
    }

    /**
     * 导出订单信息列表
     */
    @ApiOperation(value = "导出订单")
    @RequiresPermissions("order:info:export")
    @Log(title = "订单信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, OrderInfoVo orderInfo)
    {
        List<OrderInfoVo> list = orderInfoService.selectOrderInfoList(orderInfo);
        ExcelUtil<OrderInfoVo> util = new ExcelUtil<OrderInfoVo>(OrderInfoVo.class);
        util.exportExcel(response, list, "订单信息数据");
    }

    /**
     * 获取订单信息详细信息
     */
    @ApiOperation(value = "订单详情")
    @RequiresPermissions("order:info:query")
    @GetMapping(value = "/{orderId}")
    public AjaxResult getInfo(@PathVariable("orderId") Long orderId)
    {
        OrderInfoVo orderInfo = orderInfoService.selectOrderInfoByOrderId(orderId);
        Map map1 = (Map) equipClient.getInfo(orderInfo.getMaId());
        if (map1 != null) {
            Map map2 = (Map) map1.get(DATA_TAG);
            if (map2 != null) {
                String typeName = (String) map2.get("typeName");
                String groupName = (String) map2.get("groupName");
                String modelName = (String) map2.get("modelName");
                String deviceName = (String) map2.get("deviceName");
                orderInfo.setTypeName(typeName);
                orderInfo.setGroupName(groupName);
                orderInfo.setDeviceName(modelName + deviceName);
                //装备编号，图片，品牌，规格，位置addressId，月租金
                orderInfo.setDeviceCode((String) map2.get("code"));
                orderInfo.setDevicePicUrl((String) map2.get("picUrl"));
                orderInfo.setDeviceBrand((String) map2.get("brand"));
                orderInfo.setDeviceSpecification((String) map2.get("specification"));
                orderInfo.setDeviceLocation((String) map2.get("location"));
                orderInfo.setDeviceMonthLeasePrice((String) map2.get("monthLeasePrice"));
            }
        }
        return success(orderInfo);
    }

    /**
     * 新增订单信息
     */
    @ApiOperation(value = "新增订单")
    @RequiresPermissions("order:info:add")
    @Log(title = "订单信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody OrderInfoVo orderInfo)
    {
        return toAjax(orderInfoService.insertOrderInfo(orderInfo));
    }

    /**
     * 修改订单信息
     */
    @ApiOperation(value = "更新订单")
    @RequiresPermissions("order:info:edit")
    @Log(title = "订单信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody OrderInfoVo orderInfo)
    {
        return toAjax(orderInfoService.updateOrderInfo(orderInfo));
    }

    /**
     * 驳回订单
     */
    @ApiOperation(value = "驳回订单")
    @RequiresPermissions("order:info:edit")
    @Log(title = "订单信息", businessType = BusinessType.REJECT)
    @PutMapping(value = "/reject")
    public AjaxResult reject(@RequestBody OrderInfoVo orderInfo)
    {
        orderInfo.setOrderStatus(REJECT_ORDER_STATUS);
        return toAjax(orderInfoService.rejectOrderInfo(orderInfo));
    }

    /**
     * 删除订单信息
     */
    @ApiOperation(value = "删除订单")
    @RequiresPermissions("order:info:remove")
    @Log(title = "订单信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{orderIds}")
    public AjaxResult remove(@PathVariable Long[] orderIds)
    {
        return toAjax(orderInfoService.deleteOrderInfoByOrderIds(orderIds));
    }

    /**
     * 获取订单统计信息
     */
    @ApiOperation(value = "订单统计")
    @RequiresPermissions("order:info:query")
    @GetMapping(value = "/count")
    public AjaxResult count()
    {
        long count = orderInfoService.count();
        return success(count);
    }
}
