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.*;
import com.gree.mall.manager.exception.RemoteServiceException;
import com.gree.mall.manager.helper.ResponseHelper;
import com.gree.mall.manager.logic.workorder.AbnormalLogic;
import com.gree.mall.manager.logic.workorder.ChangeOrderLogic;
import com.gree.mall.manager.plus.entity.Abnormal;
import com.gree.mall.manager.plus.entity.WebsitDispatch;
import com.gree.mall.manager.utils.CommonUtils;
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.beans.factory.annotation.Autowired;
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.text.ParseException;
import java.util.List;

@Slf4j
@RestController
@Api(value = "异常配置api", tags = {"异常配置api"})
@RequestMapping(value = "/abnormal", produces = "application/json; charset=utf-8")
public class AbnormalController {

    @Autowired
    AbnormalLogic abnormalLogic;


    @ZfireList
    @PostMapping("/list")
    @ApiOperation(value = "异常配置列表")
    public ResponseHelper<IPage<AbnormalVO>> list(
            @RequestBody ZfireParamBean zfireParamBean
    ) throws RemoteServiceException {
        ZfireParamBean zfireParam = FieldUtils.supplyParam(zfireParamBean);
        IPage<AbnormalVO> increVOIPage = abnormalLogic.list(new Page(zfireParam.getPageNum(), zfireParam.getPageSize()), zfireParam);
        return ResponseHelper.success(increVOIPage, new TypeReference<AbnormalVO>() {});
    }

    @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<AbnormalVO> increVOIPage = abnormalLogic.list(new Page(zfireParam.getPageNum(), zfireParam.getPageSize()), zfireParam);
        //3.导出
        FieldUtils.exportData(increVOIPage.getRecords(), zfireParam.getExportFields(), request, response);
    }

    @PostMapping("/detail")
    @ApiOperation("配置详情")
    public ResponseHelper<Abnormal> detail(
            @ApiParam(value = "id", required = true) @RequestParam String id,
            HttpServletRequest request
    ) throws ParseException {
        Abnormal abnormalVO = abnormalLogic.detail(id);
        return ResponseHelper.success(abnormalVO);
    }

    @PostMapping("/add")
    @ApiOperation("添加配置")
    public ResponseHelper add(
            @RequestBody Abnormal abnormal,
            HttpServletRequest request
    ) throws ParseException {
        abnormalLogic.add(abnormal);
        return ResponseHelper.success();
    }

    @PostMapping("/update")
    @ApiOperation("修改配置")
    public ResponseHelper update(
            @RequestBody Abnormal abnormal,
            HttpServletRequest request
    ) throws ParseException {
        abnormalLogic.update(abnormal);
        return ResponseHelper.success();
    }


}
