package com.hzs.sale.order.controller.manager;

import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson2.JSON;
import com.hzs.common.core.annotation.AccessPermissions;
import com.hzs.common.core.annotation.Log;
import com.hzs.common.core.enums.EOperationMethod;
import com.hzs.common.core.enums.EOperationModule;
import com.hzs.common.core.enums.ESaOrderSyncStatus;
import com.hzs.common.core.utils.poi.ExcelUtil;
import com.hzs.common.core.web.controller.BaseController;
import com.hzs.common.core.web.domain.AjaxResult;
import com.hzs.common.core.web.page.TableDataInfo;
import com.hzs.common.security.utils.SecurityUtils;
import com.hzs.sale.order.dto.SaOrderSyncBodyDetailDTO;
import com.hzs.sale.order.param.SaOrderSyncParam;
import com.hzs.sale.order.service.ISaOrderSyncService;
import com.hzs.sale.order.vo.SaOrderSyncVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

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

/**
 * 订单同步记录控制器
 */
@RestController
@RequestMapping("/manage/order-sync")
public class SaOrderSyncController extends BaseController {

    @Autowired
    private ISaOrderSyncService orderSyncService;

    /**
     * 同步订单列表
     */
    @Log(module = EOperationModule.ORDER_SYNC, method = EOperationMethod.SELECT)
    @GetMapping("list")
    public TableDataInfo list(SaOrderSyncParam param) {
        startPage();
        List<SaOrderSyncVO> list = orderSyncService.queryList(param, SecurityUtils.getPkCountry());
        for (SaOrderSyncVO saOrderSyncVO : list) {
            saOrderSyncVO.setStatusVal(ESaOrderSyncStatus.getLabelByValue(saOrderSyncVO.getStatus()));
            saOrderSyncVO.setOrderProductDetail(JSONUtil.toList(saOrderSyncVO.getBodyDetail(), SaOrderSyncBodyDetailDTO.class));
        }
        return getDataTable(list);
    }


    /**
     * 更新同步状态
     */
    @Log(module = EOperationModule.ORDER_SYNC, method = EOperationMethod.UPDATE)
    @PostMapping("updateStatus")
    public AjaxResult updateStatus(@RequestBody SaOrderSyncParam param) {
        if (param.getPkId() == null) {
            return AjaxResult.error("主键ID不能为空");
        }
        if (param.getStatus() == null) {
            return AjaxResult.error("状态不能为空");
        }

        return toAjax(orderSyncService.updateStatus(param.getPkId(), param.getStatus(),
                SecurityUtils.getUserId(), SecurityUtils.getPkCountry()));
    }
}
