package com.ghy.web.controller.order;

import java.util.List;

import com.ghy.web.pojo.vo.CertNoTwoElementReq;
import com.ghy.web.service.AliCloudService;
import com.ghy.web.service.InsuranceService;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.ghy.common.annotation.Log;
import com.ghy.common.enums.BusinessType;
import com.ghy.order.domain.OrderDetailInsuranceUser;
import com.ghy.order.service.IOrderDetailInsuranceUserService;
import com.ghy.common.core.controller.BaseController;
import com.ghy.common.core.domain.AjaxResult;
import com.ghy.common.utils.poi.ExcelUtil;
import com.ghy.common.core.page.TableDataInfo;

/**
 * 订单保险人员Controller
 *
 * @author clunt
 * @date 2024-10-21
 */
@Slf4j
@Controller
@RequestMapping("/insurance/user")
public class OrderDetailInsuranceUserController extends BaseController
{
    private String prefix = "insurance/user";

    @Autowired
    private IOrderDetailInsuranceUserService orderDetailInsuranceUserService;

    @Autowired
    private  AliCloudService aliCloudService;

    @Autowired
    private InsuranceService insuranceService;

    @RequiresPermissions("worker:user:view")
    @GetMapping()
    public String user()
    {
        return prefix + "/user";
    }

    /**
     * 查询订单保险人员列表
     */
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(OrderDetailInsuranceUser orderDetailInsuranceUser)
    {
        startPage();
        List<OrderDetailInsuranceUser> list = orderDetailInsuranceUserService.selectOrderDetailInsuranceUserList(orderDetailInsuranceUser);
        return getDataTable(list);
    }

    /**
     * App查询订单保险人员列表
     */
    @PostMapping("/app/list")
    @ResponseBody
    public TableDataInfo appList(@RequestBody OrderDetailInsuranceUser orderDetailInsuranceUser)
    {
        startPage();
        List<OrderDetailInsuranceUser> list = orderDetailInsuranceUserService.selectOrderDetailInsuranceUserList(orderDetailInsuranceUser);
        return getDataTable(list);
    }


    /**
     * 导出订单保险人员列表
     */
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(OrderDetailInsuranceUser orderDetailInsuranceUser)
    {
        List<OrderDetailInsuranceUser> list = orderDetailInsuranceUserService.selectOrderDetailInsuranceUserList(orderDetailInsuranceUser);
        ExcelUtil<OrderDetailInsuranceUser> util = new ExcelUtil<OrderDetailInsuranceUser>(OrderDetailInsuranceUser.class);
        return util.exportExcel(list, "订单保险人员数据");
    }

    /**
     * 新增订单保险人员
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存订单保险人员
     */
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(@RequestBody OrderDetailInsuranceUser orderDetailInsuranceUser)
    {
        // 先校验身份证信息是否ok
        CertNoTwoElementReq req = new CertNoTwoElementReq();
        req.setCertName(orderDetailInsuranceUser.getName());
        req.setCertNo(orderDetailInsuranceUser.getIdCardNum());
        try {
            aliCloudService.certNoTwoElementVerification(req);
        } catch (Exception e) {
            log.error("身份证二要素校验失败:{}", e.getMessage(), e);
            return AjaxResult.error("请核对姓名与身份证号！");
        }
        // 将保险人员增员到原保险订单上去
        try {
            insuranceService.editInsurance(orderDetailInsuranceUser);
            orderDetailInsuranceUserService.insertOrderDetailInsuranceUser(orderDetailInsuranceUser);
            return AjaxResult.success("增员完毕，保险已生效。");
        }catch (Exception e){
            log.error("保险增员失败:{}", e.getMessage(), e);
            return AjaxResult.error("保险正生成中，可能需几秒或几分钟，请稍等即可！若超半小时可联系发单方或平台。");
        }
    }

    /**
     * 修改订单保险人员
     */
    @RequiresPermissions("worker:user:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        OrderDetailInsuranceUser orderDetailInsuranceUser = orderDetailInsuranceUserService.selectOrderDetailInsuranceUserById(id);
        mmap.put("orderDetailInsuranceUser", orderDetailInsuranceUser);
        return prefix + "/edit";
    }

    /**
     * 修改保存订单保险人员
     */
    @RequiresPermissions("worker:user:edit")
    @Log(title = "订单保险人员", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(OrderDetailInsuranceUser orderDetailInsuranceUser)
    {
        return toAjax(orderDetailInsuranceUserService.updateOrderDetailInsuranceUser(orderDetailInsuranceUser));
    }

    /**
     * 删除订单保险人员
     */
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(orderDetailInsuranceUserService.deleteOrderDetailInsuranceUserByIds(ids));
    }
}