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.notice.NoticeRecordReadBean;
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.notice.NoticeLogic;
import com.gree.mall.manager.logic.workorder.ChangeOrderLogic;
import com.gree.mall.manager.logic.workorder.IncreConfigLogic;
import com.gree.mall.manager.plus.entity.PgIncre;
import com.gree.mall.manager.plus.entity.PgOrderProductDetail;
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 javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.ParseException;
import java.util.List;

@Slf4j
@RestController
@Api(value = "增置服务配置", tags = {"增置服务配置"})
@RequestMapping(value = "/increConfig", produces = "application/json; charset=utf-8")
public class IncreConfigController {

    @Autowired
    private IncreConfigLogic increConfigLogic;


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

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

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

    @PostMapping("/add")
    @ApiOperation("添加服务配置")
    public ResponseHelper add(
            @RequestBody IncreAddOrUp increAddOrUp,
            HttpServletRequest request
    ) throws ParseException {
        increConfigLogic.add(increAddOrUp);
        return ResponseHelper.success();
    }

    @PostMapping("/update")
    @ApiOperation("修改服务配置")
    public ResponseHelper update(
            @RequestBody IncreAddOrUp increAddOrUp,
            HttpServletRequest request
    ) throws ParseException {
        increConfigLogic.update(increAddOrUp);
        return ResponseHelper.success();
    }

    @PostMapping("/upOrDown")
    @ApiOperation("上下架")
    public ResponseHelper upOrDown(
            @RequestBody IncreAddOrUp increAddOrUp,
            HttpServletRequest request
    ) throws ParseException {
        increConfigLogic.upOrDown(increAddOrUp);
        return ResponseHelper.success();
    }


}
