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

import cn.hutool.core.lang.TypeReference;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.gree.mall.manager.annotation.ZfireList;
import com.gree.mall.manager.bean.es.ESGoodsBean;
import com.gree.mall.manager.bean.es.ESGoodsVO;
import com.gree.mall.manager.enums.es.ESGoodsStatusEnum;
import com.gree.mall.manager.exception.RemoteServiceException;
import com.gree.mall.manager.helper.ResponseHelper;
import com.gree.mall.manager.logic.es.ESGoodsLogic;
import com.gree.mall.manager.plus.entity.EsGoodsOperateRecord;
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.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@Slf4j
@RestController
@Api(value = "二手商品API", tags ={"二手商品API"} )
@RequestMapping(value = "/es/goods", produces = "application/json; charset=utf-8")
public class ESGoodsController {

    @Resource
    ESGoodsLogic esGoodsLogic;

    @ZfireList
    @PostMapping("/list")
    @ApiOperation(value = "二手商品-列表")
    public ResponseHelper<IPage<ESGoodsVO>> list(
            @RequestBody ZfireParamBean zfireParamBean
    ) {
        IPage<ESGoodsVO> page = esGoodsLogic.list(zfireParamBean);
        return ResponseHelper.success(page, new TypeReference<ESGoodsVO>() {});
    }

    @PostMapping("/list/export")
    @ApiOperation("二手商品-导出")
    public void listExport(
            @RequestBody ZfireParamBean zfireParamBean,
            HttpServletRequest request,
            HttpServletResponse response
    ) throws Exception {
        //2.查询要导出的内容
        IPage<ESGoodsVO> baseVOIPage = esGoodsLogic.list(zfireParamBean);
        //3.导出
        FieldUtils.exportData(baseVOIPage.getRecords(), zfireParamBean.getExportFields(), request, response);
    }

    @PostMapping("/detail")
    @ApiOperation(value = "二手商品-详情")
    public ResponseHelper<ESGoodsBean> detail(
            @ApiParam(value = "id", required = true) @RequestParam String id
    ) throws RemoteServiceException {
        return ResponseHelper.success(esGoodsLogic.detail(id));
    }

    @PostMapping("/update")
    @ApiOperation(value = "二手商品-修改")
    public ResponseHelper update(
            @RequestBody ESGoodsBean bean
    ) throws Exception {
        esGoodsLogic.update(bean);
        return ResponseHelper.success();
    }

    @PostMapping("/batch/update/status")
    @ApiOperation(value = "二手商品-批量更新状态")
    public ResponseHelper batchUpdateStatus(
            @ApiParam(value = "id", required = true) @RequestParam List<String> ids,
            @ApiParam(value = "ON=上架 OFF=下架 SALE=已卖出", required = true) @RequestParam ESGoodsStatusEnum status
    ) throws Exception {
        if (status.getKey().equals(ESGoodsStatusEnum.SALE.getKey())) {
            throw new RemoteServiceException("接口暂不支持“已卖出”状态");
        }
        esGoodsLogic.batchUpdateStatus(ids, status);
        return ResponseHelper.success();
    }

    @PostMapping("/del")
    @ApiOperation(value = "二手商品-删除")
    public ResponseHelper del(
            @ApiParam(value = "id", required = true) @RequestParam String id
    ) throws Exception {
        esGoodsLogic.del(id);
        return ResponseHelper.success();
    }

    @PostMapping("/copy")
    @ApiOperation(value = "二手商品-克隆")
    public ResponseHelper<String> copy(
            @ApiParam(value = "id", required = true) @RequestParam String id
    ) throws Exception {
        String newId = esGoodsLogic.copy(id);
        return ResponseHelper.success(newId);
    }

    @PostMapping("/top")
    @ApiOperation(value = "二手商品-置顶")
    public ResponseHelper copy(
            @ApiParam(value = "id", required = true) @RequestParam String id,
            @ApiParam(value = "是否置顶 true=是 false=否", required = true) @RequestParam Boolean isTop
    ) throws Exception {
        esGoodsLogic.top(id, isTop);
        return ResponseHelper.success();
    }

    @PostMapping("/reply")
    @ApiOperation(value = "二手商品-回复")
    public ResponseHelper reply(
            @ApiParam(value = "id", required = true) @RequestParam String id,
            @ApiParam(value = "留言id", required = true) @RequestParam String messageId,
            @ApiParam(value = "回复内容", required = true) @RequestParam String content
    ) throws Exception {
        esGoodsLogic.reply(id, messageId, content);
        return ResponseHelper.success();
    }

    @PostMapping("/operate/list")
    @ApiOperation(value = "二手商品-操作记录列表")
    public ResponseHelper<List<EsGoodsOperateRecord>> operateList(
            @ApiParam(value = "id", required = true) @RequestParam String id
    ) {
        List<EsGoodsOperateRecord> page = esGoodsLogic.operateList(id);
        return ResponseHelper.success(page);
    }
}
