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.PolicyDetail;
import com.gree.mall.manager.bean.policy.PolicyVo;
import com.gree.mall.manager.exception.RemoteServiceException;
import com.gree.mall.manager.helper.ResponseHelper;
import com.gree.mall.manager.logic.policy.PolicyLogic;
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 = "/policyManager", produces = "application/json; charset=utf-8")
public class PolicyController {

    @Autowired
    private PolicyLogic policyLogic;


    @ZfireList
    @PostMapping("/list")
    @ApiOperation(value = "保险方案管理列表")
    public ResponseHelper<IPage<PolicyVo>> list(
            @RequestBody ZfireParamBean zfireParamBean
    ) throws RemoteServiceException {
        ZfireParamBean zfireParam = FieldUtils.supplyParam(zfireParamBean);
        IPage<PolicyVo> increVOIPage = policyLogic.list(new Page(zfireParam.getPageNum(), zfireParam.getPageSize()), zfireParam);
        return ResponseHelper.success(increVOIPage, new TypeReference<PolicyVo>() {});
    }

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

    @PostMapping("/detail")
    @ApiOperation("保险方案管理详情")
    public ResponseHelper<PolicyDetail> detail(
            @ApiParam(value = "id", required = true) @RequestParam String id,
            HttpServletRequest request
    ) throws ParseException {
        PolicyDetail policyDetail = policyLogic.detail(id);
        return ResponseHelper.success(policyDetail);
    }

    @PostMapping("/add")
    @ApiOperation("添加保险方案管理")
    public ResponseHelper add(
            @RequestBody PolicyDetail policyDetail,
            HttpServletRequest request
    ) throws ParseException {
        policyLogic.add(policyDetail);
        return ResponseHelper.success();
    }

    @PostMapping("/update")
    @ApiOperation("修改保险方案管理")
    public ResponseHelper update(
            @RequestBody PolicyDetail policyDetail,
            HttpServletRequest request
    ) throws ParseException {
        policyLogic.update(policyDetail);
        return ResponseHelper.success();
    }


    @PostMapping("/del")
    @ApiOperation("删除")
    public ResponseHelper del(
            @ApiParam(value = "id", required = true) @RequestParam String id,
            HttpServletRequest request
    ) throws ParseException {
        policyLogic.del(id);
        return ResponseHelper.success();
    }



}
