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.dto.ContractInfoDto;
import com.bonus.zlpt.common.core.domain.order.vo.OrderDetailsVo;
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.common.security.utils.SecurityUtils;
import com.bonus.zlpt.order.feign.EquipClient;
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.bonus.zlpt.common.core.domain.order.OrderDetails;
import com.bonus.zlpt.order.service.IOrderDetailsService;
import static com.bonus.zlpt.common.core.web.domain.AjaxResult.DATA_TAG;

/**
 * 订单详细Controller
 * 
 * @author xsheng
 * @date 2023-12-01
 */
@Deprecated
@RestController
@RequestMapping("/details")
public class OrderDetailsController extends BaseController
{
    @Autowired
    private IOrderDetailsService orderDetailsService;
    @Autowired
    EquipClient equipClient;

    /**
     * 查询订单详细列表
     */
    @RequiresPermissions("order:details:list")
    @GetMapping("/list")
    public TableDataInfo list(OrderDetails orderDetails)
    {
        startPage();
        List<OrderDetails> list = orderDetailsService.selectOrderDetailsList(orderDetails);
        return getDataTable(list);
    }

    /**
     * 导出订单详细列表
     */
    @RequiresPermissions("order:details:export")
    @Log(title = "订单详细", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, OrderDetails orderDetails)
    {
        List<OrderDetails> list = orderDetailsService.selectOrderDetailsList(orderDetails);
        ExcelUtil<OrderDetails> util = new ExcelUtil<OrderDetails>(OrderDetails.class);
        util.exportExcel(response, list, "订单详细数据");
    }

    /**
     * 获取订单详细详细信息
     */
    @RequiresPermissions("order:details:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        OrderDetailsVo orderDetails = orderDetailsService.selectOrderDetailsById(id);
        Map map1 = (Map) equipClient.getInfo(orderDetails.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");
                orderDetails.setTypeName(typeName);
                orderDetails.setGroupName(groupName);
                orderDetails.setDeviceName(modelName + deviceName);
            }
        }
        return success(orderDetails);
    }

    /**
     * 新增订单详细
     */
    @RequiresPermissions("order:details:add")
    @Log(title = "订单详细", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody OrderDetailsVo orderDetails)
    {
        return toAjax(orderDetailsService.insertOrderDetails(orderDetails));
    }

    /**
     * 修改订单详细
     */
    @RequiresPermissions("order:details:edit")
    @Log(title = "订单详细", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody OrderDetailsVo orderDetails)
    {
        return toAjax(orderDetailsService.updateOrderDetails(orderDetails));
    }

    /**
     * 删除订单详细
     */
    @RequiresPermissions("order:details:remove")
    @Log(title = "订单详细", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(orderDetailsService.deleteOrderDetailsByIds(ids));
    }

    /**
     * 新增合同文件
     * @param dto
     * @return
     */
    @PostMapping("/addContract")
    public AjaxResult addContract(@RequestBody ContractInfoDto dto)
    {
        Long userId = SecurityUtils.getUserId();
        dto.getContractDto().setUserId(userId.intValue());
        return toAjax(orderDetailsService.addContract(dto));
    }
}
