package com.game.business.controller;

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

import com.alibaba.fastjson2.JSONObject;
import com.game.business.domain.LiveLive;
import com.game.business.domain.ShopSubOrder;
import com.game.business.dto.OrderSendDTO;
import com.game.business.service.IShopSubOrderService;
import com.game.business.util.im.HttpClientUtils;
import com.game.business.util.im.TencentCloudImUtil;
import com.game.common.core.domain.R;
import com.game.system.service.ISysConfigService;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.lang3.StringUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
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.game.common.annotation.Log;
import com.game.common.core.controller.BaseController;
import com.game.common.core.domain.AjaxResult;
import com.game.common.enums.BusinessType;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import com.game.business.domain.ShopBusinessOrder;
import com.game.business.service.IShopBusinessOrderService;
import com.game.common.utils.poi.ExcelUtil;
import com.game.common.core.page.TableDataInfo;

/**
 * 购物订单Controller
 * 
 * @author game
 * @date 2024-09-06
 */
@RestController
@RequestMapping("/business/order")
@Api(value = "ShopBusinessOrderController", description = "购物订单接口", tags = {"购物订单"})
@Log4j2
public class ShopBusinessOrderController extends BaseController
{
    @Autowired
    private IShopBusinessOrderService shopBusinessOrderService;

    @Autowired
    private IShopSubOrderService shopSubOrderService;

    @Autowired
    private ISysConfigService sysConfigService;

    /**
     * 查询购物订单列表
     */
    @PreAuthorize("@ss.hasPermi('business:order:list')")
    @GetMapping("/list")
    @ApiOperation(value = "查询购物订单列表", notes = "获取购物订单列表")
    public TableDataInfo<ShopBusinessOrder> list(ShopBusinessOrder shopBusinessOrder)
    {
        startPage();
        List<ShopBusinessOrder> list = shopBusinessOrderService.selectShopBusinessOrderList(shopBusinessOrder);
        if(null != list && list.size() > 0){
            list.forEach(e->{
                ShopSubOrder query = new ShopSubOrder();
                query.setBusinessOrderId(e.getId());
                List<ShopSubOrder> shopSubOrders = shopSubOrderService.selectShopSubOrderList(query);
                if(null != shopSubOrders && shopSubOrders.size() > 0){
                    String productNames = "";
                    for(ShopSubOrder shopSubOrder : shopSubOrders){
                        if(StringUtils.isNotBlank(productNames)){
                            productNames = productNames.concat(",");
                        }
                        productNames = productNames.concat(shopSubOrder.getGoodsName());
                    }
                    e.setProductNames(productNames);
                }
            });
        }
        return getDataTable(list);
    }

    /**
     * 导出购物订单列表
     */
    @PreAuthorize("@ss.hasPermi('business:order:export')")
    @Log(title = "购物订单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ApiOperation(value = "导出购物订单列表", notes = "导出购物订单列表")
    public void export(HttpServletResponse response, ShopBusinessOrder shopBusinessOrder)
    {
        List<ShopBusinessOrder> list = shopBusinessOrderService.selectShopBusinessOrderList(shopBusinessOrder);
        ExcelUtil<ShopBusinessOrder> util = new ExcelUtil<ShopBusinessOrder>(ShopBusinessOrder.class);
        util.exportExcel(response, list, "购物订单数据");
    }

    /**
     * 获取购物订单详细信息
     */
    @PreAuthorize("@ss.hasPermi('business:order:query')")
    @GetMapping(value = "/{id}")
    @ApiOperation(value = "获取购物订单详细信息", notes = "获取购物订单详细信息")
    public R<ShopBusinessOrder> getInfo(@PathVariable("id") Long id)
    {
        return R.ok(shopBusinessOrderService.selectShopBusinessOrderById(id));
    }

    /**
     * 新增购物订单
     */
    @PreAuthorize("@ss.hasPermi('business:order:add')")
    @Log(title = "购物订单", businessType = BusinessType.INSERT)
    @ApiOperation(value = "新增购物订单", notes = "新增购物订单")
    @PostMapping
    public R add(@RequestBody ShopBusinessOrder shopBusinessOrder)
    {
        return R.ok(shopBusinessOrderService.insertShopBusinessOrder(shopBusinessOrder));
    }

    /**
     * 修改购物订单
     */
    @PreAuthorize("@ss.hasPermi('business:order:edit')")
    @Log(title = "购物订单", businessType = BusinessType.UPDATE)
    @ApiOperation(value = "修改购物订单", notes = "修改购物订单")
    @PutMapping
    public R edit(@RequestBody ShopBusinessOrder shopBusinessOrder)
    {
        return R.ok(shopBusinessOrderService.updateShopBusinessOrder(shopBusinessOrder));
    }

    /**
     * 删除购物订单
     */
    @PreAuthorize("@ss.hasPermi('business:order:remove')")
    @Log(title = "购物订单", businessType = BusinessType.DELETE)
    @ApiOperation(value = "删除购物订单", notes = "删除购物订单")
	@DeleteMapping("/{ids}")
    public R remove(@PathVariable Long[] ids)
    {
        return R.ok(shopBusinessOrderService.deleteShopBusinessOrderByIds(ids));
    }

    /**
     * 发货
     */
    @PreAuthorize("@ss.hasPermi('business:order:send')")
    @Log(title = "发货", businessType = BusinessType.UPDATE)
    @PostMapping("/send")
    @ApiOperation(value = "发货", notes = "发货")
    public R<String> openLive(HttpServletResponse response, @RequestBody OrderSendDTO orderSendDTO)
    {
        String url = sysConfigService.selectConfigByKey("sys_live_apiurl").concat("/api/shopOrderOther/confirmSent");
        Map<String, String> map = new HashMap<>();
        map.put("id", String.valueOf(orderSendDTO.getId()));
        map.put("logisticsNum", orderSendDTO.getLogisticsNum());
        map.put("logisticsName", orderSendDTO.getLogisticsName());

        String res = HttpClientUtils.ajaxPost(url,map);
        if(StringUtils.isBlank(res) || !JSONObject.parseObject(res).getString("code").equals("1")){
            log.info("失败===============》》".concat(res));
            return R.fail("发货失败");
        }
        return R.ok("发货成功","发货成功");
    }
}
