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

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.ZfireList;
import com.gree.mall.manager.bean.engin.OrderEnginBaseBean;
import com.gree.mall.manager.bean.engin.OrderEnginBaseVO;
import com.gree.mall.manager.exception.RemoteServiceException;
import com.gree.mall.manager.helper.ResponseHelper;
import com.gree.mall.manager.logic.engin.OrderEnginBaseLogic;
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.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

@Slf4j
@RestController
@Api(value = "工程基础资料API", tags = {"工程基础资料API"})
@Validated
@RequestMapping(value = "/order/engin/base", produces = "application/json; charset=utf-8")
public class OrderEnginBaseController {

    @Resource
    OrderEnginBaseLogic orderEnginBaseLogic;

    @ZfireList
    @PostMapping("/list")
    @ApiOperation(value = "工程基础资料-列表")
    public ResponseHelper<Page<OrderEnginBaseVO>> list(
            @RequestBody ZfireParamBean zfireParamBean
    ) {
        IPage<OrderEnginBaseVO> vo = orderEnginBaseLogic.list(zfireParamBean);
        return ResponseHelper.success(vo, new TypeReference<OrderEnginBaseVO>() {});
    }

    @PostMapping("/list/export")
    @ApiOperation(value = "工程基础资料-导出")
    public void listExport(@RequestBody ZfireParamBean zfireParamBean, HttpServletRequest request, HttpServletResponse response) throws Exception {
        //1.组装查询条件
        ZfireParamBean zfireParam = FieldUtils.supplyParam(zfireParamBean);
        //2.查询要导出的内容
        IPage<OrderEnginBaseVO> page = orderEnginBaseLogic.list(zfireParamBean);
        //3.导出
        FieldUtils.exportData(page.getRecords(), zfireParam.getExportFields(), request, response);
    }

    @PostMapping("/detail")
    @ApiOperation(value = "工程基础资料-详情")
    public ResponseHelper<OrderEnginBaseBean> detail(
            @ApiParam(value = "id", required = true) @RequestParam String id
    ) throws RemoteServiceException {
        OrderEnginBaseBean bean = orderEnginBaseLogic.detail(id);
        return ResponseHelper.success(bean);
    }

    @PostMapping("/save")
    @ApiOperation(value = "工程基础资料-保存")
    public ResponseHelper save(
           @Valid @RequestBody OrderEnginBaseBean bean
    ) throws Exception {

        orderEnginBaseLogic.save(bean);

        return ResponseHelper.success();
    }

}
