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

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.policy.*;
import com.gree.mall.manager.exception.RemoteServiceException;
import com.gree.mall.manager.helper.ResponseHelper;
import com.gree.mall.manager.logic.policy.AgreementLogic;
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;

@Slf4j
@RestController
@Api(value = "保险协议配置", tags = {"保险协议配置"})
@RequestMapping(value = "/agreement", produces = "application/json; charset=utf-8")
public class AgreementController {

    @Autowired
    private AgreementLogic agreementLogic;


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

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

    @PostMapping("/detail")
    @ApiOperation("保险协议配置详情")
    public ResponseHelper<AgreementDetail> detail(
            @ApiParam(value = "id", required = true) @RequestParam String id,
            HttpServletRequest request
    ) throws ParseException {
        AgreementDetail agreementDetail = agreementLogic.detail(id);
        return ResponseHelper.success(agreementDetail);
    }

    @PostMapping("/add")
    @ApiOperation("添加保险协议配置")
    public ResponseHelper add(
            @RequestBody AgreementAddUp agreementAddUp,
            HttpServletRequest request
    ) throws ParseException {
        agreementLogic.add(agreementAddUp);
        return ResponseHelper.success();
    }

    @PostMapping("/update")
    @ApiOperation("修改保险协议配置")
    public ResponseHelper update(
            @RequestBody AgreementAddUp agreementAddUp,
            HttpServletRequest request
    ) throws ParseException {
        agreementLogic.update(agreementAddUp);
        return ResponseHelper.success();
    }


    @PostMapping("/del")
    @ApiOperation("删除保险协议配置")
    public ResponseHelper del(
            @ApiParam(value = "id", required = true) @RequestParam String id,
            HttpServletRequest request
    ) throws ParseException {
        agreementLogic.del(id);
        return ResponseHelper.success();
    }
}
