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

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.workorder.ExamineProjectConfigVO;
import com.gree.mall.manager.bean.workorder.ExamineProjectVO;
import com.gree.mall.manager.exception.RemoteServiceException;
import com.gree.mall.manager.helper.ResponseHelper;
import com.gree.mall.manager.logic.workorder.ExamineProjectConfigLogic;
import com.gree.mall.manager.plus.entity.PgExamineProject;
import com.gree.mall.manager.plus.entity.PgExamineProjectConfig;
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.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@RequiredArgsConstructor
@Slf4j
@RestController
@Api(value = "考核项目API", tags = {"考核项目API"})
@RequestMapping(value = "/examine/project", produces = "application/json; charset=utf-8")
public class ExamineProjectConfigController {

    private final ExamineProjectConfigLogic examineProjectConfigLogic;

    @ZfireList
    @PostMapping("/config/list")
    @ApiOperation(value = "考核项目配置-列表")
    public ResponseHelper<Page<ExamineProjectConfigVO>> configList(
            @RequestBody ZfireParamBean zfireParamBean
    ) {
        IPage<ExamineProjectConfigVO> orderSmallTypeVO = examineProjectConfigLogic.configList(zfireParamBean);
        return ResponseHelper.success(orderSmallTypeVO, new TypeReference<ExamineProjectConfigVO>() {});
    }

    @PostMapping("/config/list/export")
    @ApiOperation(value = "考核项目配置-导出")
    public void configListExport(@RequestBody ZfireParamBean zfireParamBean, HttpServletRequest request, HttpServletResponse response) throws Exception {
        //1.组装查询条件
        ZfireParamBean zfireParam = FieldUtils.supplyParam(zfireParamBean);
        //2.查询要导出的内容
        IPage<ExamineProjectConfigVO> page = examineProjectConfigLogic.configList(zfireParamBean);
        //3.导出
        FieldUtils.exportData(page.getRecords(), zfireParam.getExportFields(), request, response);
    }

    @PostMapping("/config/detail")
    @ApiOperation(value = "考核项目配置-详情")
    public ResponseHelper<PgExamineProjectConfig> configDetail(
            @ApiParam(value = "id", required = true) @RequestParam String id
    ) throws RemoteServiceException {
        return ResponseHelper.success(examineProjectConfigLogic.configDetail(id));
    }

    @PostMapping("/config/add")
    @ApiOperation(value = "考核项目配置-添加")
    public ResponseHelper<PgExamineProjectConfig> configAdd(
            @RequestBody PgExamineProjectConfig config
    ) throws Exception {

        examineProjectConfigLogic.configAdd(config);

        return ResponseHelper.success();
    }

    @PostMapping("/config/edit")
    @ApiOperation(value = "考核项目配置-编辑")
    public ResponseHelper<PgExamineProjectConfig> configEdit(
            @RequestBody PgExamineProjectConfig config
    ) throws Exception {

        examineProjectConfigLogic.configEdit(config);

        return ResponseHelper.success();
    }

    @PostMapping("/config/delete")
    @ApiOperation("考核项目配置-删除")
    public ResponseHelper configDelete(
            @ApiParam(value = "id", required = true) @RequestParam String id
    ) {
        examineProjectConfigLogic.configDelete(id);
        return ResponseHelper.success();
    }

    @ZfireList
    @PostMapping("/list")
    @ApiOperation(value = "奖惩单-列表")
    public ResponseHelper<Page<ExamineProjectVO>> list(
            @RequestBody ZfireParamBean zfireParamBean
    ) {
        IPage<ExamineProjectVO> page = examineProjectConfigLogic.list(zfireParamBean);
        return ResponseHelper.success(page, new TypeReference<ExamineProjectVO>() {});
    }

    @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<ExamineProjectVO> page = examineProjectConfigLogic.list(zfireParamBean);
        //3.导出
        FieldUtils.exportData(page.getRecords(), zfireParam.getExportFields(), request, response);
    }

    @PostMapping("/detail")
    @ApiOperation(value = "奖惩单-详情")
    public ResponseHelper<PgExamineProject> detail(
            @ApiParam(value = "id", required = true) @RequestParam String id
    ) throws RemoteServiceException {
        return ResponseHelper.success(examineProjectConfigLogic.detail(id));
    }

    @PostMapping("/add")
    @ApiOperation(value = "奖惩单-添加")
    public ResponseHelper<PgExamineProject> add(
            @RequestBody PgExamineProject project
    ) throws Exception {

        examineProjectConfigLogic.add(project);

        return ResponseHelper.success();
    }


}
