package com.yc.ship.module.trade.controller.admin.insurance;

import com.yc.ship.framework.common.pojo.CommonResult;
import com.yc.ship.framework.common.pojo.PageParam;
import com.yc.ship.framework.common.pojo.PageResult;
import com.yc.ship.framework.common.util.object.BeanUtils;
import com.yc.ship.framework.excel.core.util.ExcelUtils;
import com.yc.ship.framework.operatelog.core.annotations.OperateLog;
import com.yc.ship.module.trade.api.insurance.dto.InsuranceApplyReqDTO;
import com.yc.ship.module.trade.controller.admin.insurance.vo.InsurancePageReqVO;
import com.yc.ship.module.trade.controller.admin.insurance.vo.InsuranceRespVO;
import com.yc.ship.module.trade.dal.dataobject.insurance.InsuranceDO;
import com.yc.ship.module.trade.service.insurance.InsuranceService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

import static com.yc.ship.framework.common.pojo.CommonResult.success;
import static com.yc.ship.framework.operatelog.core.enums.OperateTypeEnum.API;
import static com.yc.ship.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;


@Tag(name = "管理后台 - 订单保险信息")
@RestController
@RequestMapping("/trade/insurance")
@Validated
public class InsuranceController {

    @Resource
    private InsuranceService insuranceService;


    @GetMapping("/get")
    @Operation(summary = "获得订单保险信息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('trade:insurance:query')")
    public CommonResult<InsuranceRespVO> getInsurance(@RequestParam("id") Long id) {
        InsuranceDO insurance = insuranceService.getInsurance(id);
        return success(BeanUtils.toBean(insurance, InsuranceRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得订单保险信息分页")
    @PreAuthorize("@ss.hasPermission('trade:insurance:query')")
    public CommonResult<PageResult<InsuranceRespVO>> getInsurancePage(@Valid InsurancePageReqVO pageReqVO) {
        PageResult<InsuranceRespVO> pageResult = insuranceService.getInsurancePage(pageReqVO);
        return success(pageResult);
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出订单保险信息 Excel")
    @PreAuthorize("@ss.hasPermission('trade:insurance:export')")
    @OperateLog(type = EXPORT,enable = false)
    public void exportInsuranceExcel(@Valid InsurancePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<InsuranceRespVO> list = insuranceService.getInsurancePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "订单保险信息.xls", "数据", InsuranceRespVO.class,
                        list);
    }

    @GetMapping("/applyInsurance")
    @Operation(summary = "申请投保")
    @OperateLog(type = API)
    @PreAuthorize("@ss.hasPermission('trade:insurance:apply')")
    public CommonResult<Boolean> applyInsurance(@RequestParam("orderId") Long orderId) {
        insuranceService.applyInsurance(orderId);
        return success(true);
    }

    @GetMapping("/applyInsuranceNew")
    @Operation(summary = "申请投保新（个保）")
    @OperateLog(type = API)
    @PreAuthorize("@ss.hasPermission('trade:insurance:apply')")
    public CommonResult<Boolean> applyInsuranceNew(@RequestParam("id") Long id) {
        insuranceService.applyInsuranceNew(id);
        return success(true);
    }
    @GetMapping("/queryInsurance")
    @Operation(summary = "查询保单并返回信息")
    @OperateLog(type = API)
    @PreAuthorize("@ss.hasPermission('trade:insurance:query')")
    public CommonResult<String> queryInsurance(@RequestParam("id") Long id) {
        String res = insuranceService.queryInsurance(id);
        return success(res);
    }

    @GetMapping("/queryEpolicy")
    @Operation(summary = "根据id获取电子保单")
    @OperateLog(type = API)
    @PreAuthorize("@ss.hasPermission('trade:insurance:query')")
    public CommonResult queryEpolicyList(@RequestParam("id") Long id) {
        insuranceService.queryInsuranceByOrderId(id);
        return success(true);
    }

    @GetMapping("/queryEpolicyAll")
    @Operation(summary = "根据id获取电子保单")
    @OperateLog(type = API)
    public CommonResult queryEpolicyListAll() {
        insuranceService.queryInsuranceQuey();
        return success(true);
    }


    @GetMapping("/cancel")
    @Operation(summary = "退保")
    @OperateLog(type = API)
    @PreAuthorize("@ss.hasPermission('trade:insurance:cancel')")
    public CommonResult<Boolean>  cancelInsurance(@RequestParam("id") Long id){
        insuranceService.applyCancelInsurance(id);
        return success(true);
    }
}