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.PolicyOrderEmVO;
import com.gree.mall.manager.bean.policy.PolicyOrderInVO;
import com.gree.mall.manager.bean.policy.PolicyOrderVO;
import com.gree.mall.manager.exception.RemoteServiceException;
import com.gree.mall.manager.helper.ResponseHelper;
import com.gree.mall.manager.logic.policy.PolicyOrderLogic;
import com.gree.mall.manager.plus.entity.PolicyOrder;
import com.gree.mall.manager.utils.excel.ExcelUtils;
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 java.io.IOException;
import java.text.ParseException;
import java.util.List;

@Slf4j
@RestController
@Api(value = "保险API", tags = {"保险API"})
@RequestMapping(value = "/policyOrder", produces = "application/json; charset=utf-8")
public class PolicyOrderController {

    @Autowired
    private PolicyOrderLogic policyOrderLogic;


    @ZfireList
    @PostMapping("/list")
    @ApiOperation(value = "意外保险订单列表")
    public ResponseHelper<IPage<PolicyOrderVO>> list(
            @RequestBody ZfireParamBean zfireParamBean
    ) throws RemoteServiceException {
        ZfireParamBean zfireParam = FieldUtils.supplyParam(zfireParamBean);
        IPage<PolicyOrderVO> policyOrderVOIPage = policyOrderLogic.listPolicyOrder(new Page(zfireParam.getPageNum(), zfireParam.getPageSize()), zfireParam);
        return ResponseHelper.success(policyOrderVOIPage, new TypeReference<PolicyOrderVO>() {});
    }

    @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<PolicyOrderVO> policyOrderVOIPage = policyOrderLogic.listPolicyOrder(new Page(zfireParam.getPageNum(), zfireParam.getPageSize()), zfireParam);
        //3.导出
        FieldUtils.exportData(policyOrderVOIPage.getRecords(), zfireParam.getExportFields(), request, response);
    }

    @ZfireList
    @PostMapping("/listEm")
    @ApiOperation(value = "雇主险订单列表")
    public ResponseHelper<IPage<PolicyOrderEmVO>> listEm(
            @RequestBody ZfireParamBean zfireParamBean
    ) throws RemoteServiceException {
        ZfireParamBean zfireParam = FieldUtils.supplyParam(zfireParamBean);
        IPage<PolicyOrderEmVO> policyOrderVOIPage = policyOrderLogic.listEm(new Page(zfireParam.getPageNum(), zfireParam.getPageSize()), zfireParam);
        return ResponseHelper.success(policyOrderVOIPage, new TypeReference<PolicyOrderEmVO>() {});
    }

    @PostMapping("/listEm/export")
    @ApiOperation(value = "雇主险订单列表导出")
    public void listEmExport(
            @RequestBody ZfireParamBean zfireParamBean,
            HttpServletRequest request,
            HttpServletResponse response
    ) throws Exception {
        //1.组装查询条件
        ZfireParamBean zfireParam = FieldUtils.supplyParam(zfireParamBean);
        //2.查询要导出的内容
        IPage<PolicyOrderEmVO> policyOrderVOIPage = policyOrderLogic.listEm(new Page(zfireParam.getPageNum(), zfireParam.getPageSize()), zfireParam);
        //3.导出
        FieldUtils.exportData(policyOrderVOIPage.getRecords(), zfireParam.getExportFields(), request, response);
    }


    @PostMapping("/importEm")
    @ApiOperation(value = "雇主险导入")
    public ResponseHelper importEm(
            @RequestParam MultipartFile file
    ) throws RemoteServiceException, IOException {
        List<Object> objects = ExcelUtils.importExcel(file);
        policyOrderLogic.importEm(objects);
        return ResponseHelper.success();
    }

    @PostMapping("/detail")
    @ApiOperation("保险险订单详情")
    public ResponseHelper<PolicyOrder> detail(
            @ApiParam(value = "id", required = true) @RequestParam String id,
            HttpServletRequest request
    ) throws ParseException {
        PolicyOrder policyDetail = policyOrderLogic.detail(id);
        return ResponseHelper.success(policyDetail);
    }

    @PostMapping("/update")
    @ApiOperation("保险订单修改")
    public ResponseHelper add(
            @RequestBody PolicyOrder policyOrder,
            HttpServletRequest request
    ) throws ParseException {
        policyOrderLogic.update(policyOrder);
        return ResponseHelper.success();
    }



    @ZfireList
    @PostMapping("/listIn")
    @ApiOperation(value = "工伤险列表")
    public ResponseHelper<IPage<PolicyOrderInVO>> listIn(
            @RequestBody ZfireParamBean zfireParamBean
    ) throws RemoteServiceException {
        ZfireParamBean zfireParam = FieldUtils.supplyParam(zfireParamBean);
        IPage<PolicyOrderInVO> policyOrderVOIPage = policyOrderLogic.listIn(new Page(zfireParam.getPageNum(), zfireParam.getPageSize()), zfireParam);
        return ResponseHelper.success(policyOrderVOIPage, new TypeReference<PolicyOrderInVO>() {});
    }

    @PostMapping("/listIn/export")
    @ApiOperation(value = "工伤险订单列表导出")
    public void listInExport(
            @RequestBody ZfireParamBean zfireParamBean,
            HttpServletRequest request,
            HttpServletResponse response
    ) throws Exception {
        //1.组装查询条件
        ZfireParamBean zfireParam = FieldUtils.supplyParam(zfireParamBean);
        //2.查询要导出的内容
        IPage<PolicyOrderInVO> policyOrderVOIPage = policyOrderLogic.listIn(new Page(zfireParam.getPageNum(), zfireParam.getPageSize()), zfireParam);
        //3.导出
        FieldUtils.exportData(policyOrderVOIPage.getRecords(), zfireParam.getExportFields(), request, response);
    }


    @PostMapping("/import")
    @ApiOperation(value = "工伤保险导入")
    public ResponseHelper wait2Del(
            @RequestParam MultipartFile file
    ) throws RemoteServiceException, IOException {
        List<Object> objects = ExcelUtils.importExcel(file);
        policyOrderLogic.importData(objects);
        return ResponseHelper.success();
    }



}
