package com.gree.mall.manager.controller.workerorder;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.lang.TypeReference;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gree.mall.manager.annotation.ApiNotAuth;
import com.gree.mall.manager.annotation.ZfireList;
import com.gree.mall.manager.bean.admin.AdminUserCom;
import com.gree.mall.manager.bean.es.OrderBaseEs;
import com.gree.mall.manager.bean.listvo.param.WorkOrderZfireParam;
import com.gree.mall.manager.bean.listvo.workorder.OrderBase2VO;
import com.gree.mall.manager.bean.listvo.workorder.OrderBaseVO;
import com.gree.mall.manager.bean.workorder.OrderAddBean2DTO;
import com.gree.mall.manager.bean.workorder.OrderAddBeanDTO;
import com.gree.mall.manager.bean.workorder.OrderSettleDetailBean;
import com.gree.mall.manager.constant.Constant;
import com.gree.mall.manager.enums.workorder.OrderFlagEnum;
import com.gree.mall.manager.es.OrderBaseEsRepository;
import com.gree.mall.manager.exception.RemoteServiceException;
import com.gree.mall.manager.helper.ResponseHelper;
import com.gree.mall.manager.logic.common.CommonLogic;
import com.gree.mall.manager.logic.workorder.OrderBaseEsLogic;
import com.gree.mall.manager.logic.workorder.OrderBaseExcelLogic;
import com.gree.mall.manager.logic.workorder.OrderBaseLogic;
import com.gree.mall.manager.logic.workorder.OrderLogLogic;
import com.gree.mall.manager.plus.entity.PgOrderBase;
import com.gree.mall.manager.plus.entity.PgOrderOperatorLog;
import com.gree.mall.manager.plus.entity.PgOrderProduct;
import com.gree.mall.manager.plus.entity.PgOrderWorker;
import com.gree.mall.manager.utils.excel.ExcelUtils;
import com.gree.mall.manager.zfire.bean.ZfireParamBean;
import com.gree.mall.manager.zfire.util.FieldUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.integration.redis.util.RedisLockRegistry;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock;

@RequiredArgsConstructor
@Slf4j
@RestController
@Api(value = "ES工单API", tags = {"ES工单API"})
@RequestMapping(value = "/orderEs/base", produces = "application/json; charset=utf-8")
public class ESOrderBaseController {

    private final OrderBaseEsLogic orderBaseEsLogic;





    @ZfireList
    @PostMapping("/list")
    @ApiOperation(value = "列表")
    public ResponseHelper<Page<OrderBaseVO>> list(
            @RequestBody WorkOrderZfireParam workOrderZfireParam
    ) {
        IPage<OrderBaseVO> orderBaseVOIPage = orderBaseEsLogic.orderBaseEsList(workOrderZfireParam);
        return ResponseHelper.success(orderBaseVOIPage, new TypeReference<OrderBaseVO>() {});
    }

    @PostMapping("/list/export")
    @ApiOperation(value = "导出")
    public void listExport(@RequestBody WorkOrderZfireParam zfireParamBean, HttpServletRequest request, HttpServletResponse response) throws Exception {
        //1.组装查询条件
        ZfireParamBean zfireParam = FieldUtils.supplyParam(zfireParamBean);
        //2.查询要导出的内容
        zfireParamBean.setPageNum(1);
        zfireParamBean.setPageSize(30000);
        IPage<OrderBaseVO> orderBaseVOIPage = orderBaseEsLogic.orderBaseEsList(zfireParamBean);
        //3.导出
        FieldUtils.exportData(orderBaseVOIPage.getRecords(), zfireParam.getExportFields(), request, response);
    }

    @PostMapping("/status/count")
    @ApiOperation(value = "按工单状态统计")
    public ResponseHelper<List<Map<String,Object>>> countStatus(
            @RequestParam(required = false) String startTime,
            @RequestParam(required = false) String endTime,
            @RequestParam(required = false) String orderSmallType,
            @RequestParam(required = false) String orderSmallTypeText
    ) {
        List<Map<String, Object>> maps = orderBaseEsLogic.countStatus(startTime,endTime,orderSmallType,orderSmallTypeText);
        return ResponseHelper.success(maps);
    }


    @ApiNotAuth
    @PostMapping("test")
    public ResponseHelper test() throws IllegalAccessException {
        orderBaseEsLogic.test();
        return ResponseHelper.success();
    }

    @ApiNotAuth
    @PostMapping("del")
    public ResponseHelper del() throws IllegalAccessException {
        orderBaseEsLogic.del();
        return ResponseHelper.success();
    }

}
