package cn.timer.api.controller.insure;

import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Random;

import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.lang.tree.TreeNodeConfig;
import cn.hutool.core.lang.tree.TreeUtil;
import cn.timer.api.bean.insure.InsureApplicant;
import cn.timer.api.bean.insure.InsureProvince;
import cn.timer.api.bean.qyzx.QyzxEntInfoM;
import cn.timer.api.config.annotation.CurrentUser;
import cn.timer.api.config.annotation.UserBean;
import cn.timer.api.config.exception.CustomException;
import cn.timer.api.dao.insure.InsureApplicantMapper;
import cn.timer.api.dto.insure.InsureDto;
import cn.timer.api.utils.HttpUtils;
import cn.timer.api.utils.Result;
import cn.timer.api.utils.ResultUtil;
import com.alibaba.druid.util.Base64;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.google.common.collect.Maps;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import org.apache.commons.codec.digest.DigestUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import org.springframework.web.multipart.MultipartFile;


/**
 * 投保人
 *
 * @author wgd
 * @email 862422848@qq.com
 * @date 2022-03-07 17:02:46
 */
@Api(tags = "8.0保险列表")
@RestController
@Transactional
@RequestMapping(value = "/insureApplicant", produces = {"application/json"})
public class InsureApplicantController {
    private static final Logger log = LoggerFactory.getLogger(InsureApplicantController.class);

    @PostMapping("/insureApplicationSetting")
    @ApiOperation(value = "设置投保人", httpMethod = "POST", notes = "投保申请")
    @Transactional(rollbackFor = Exception.class)
    public Result<Object> insureApplicationSetting(@RequestBody InsureApplicant params) {
        try {
            InsureApplicant insureApplicant = InsureApplicant.builder().build().selectOne(new QueryWrapper<InsureApplicant>().lambda().eq(InsureApplicant::getOrgCode, params.getOrgCode()));
            params.setId(insureApplicant.getId());
            params.updateById();
            return ResultUtil.data(params);
        } catch (Exception e) {
            log.error("设置投保人异常:",e);
            throw new CustomException("设置投保人异常");
        }
    }


    @GetMapping("/getApplicantAdmin")
    @ApiOperation(value = "获取投保人--运营后台", httpMethod = "GET", notes = "获取投保人")
    @Transactional(rollbackFor = Exception.class)
    public Result<Object> getApplicant(@RequestParam("orgCode") Integer orgCode) {
        try {
            InsureApplicant insureApplicant = InsureApplicant.builder().build().selectOne(new QueryWrapper<InsureApplicant>().lambda().eq(InsureApplicant::getOrgCode, orgCode));
            if (insureApplicant == null) {
                QyzxEntInfoM qyzxEntInfoM = QyzxEntInfoM.builder().id(orgCode).build().selectById();
                insureApplicant = getInsureApplicant(qyzxEntInfoM);
                insureApplicant.setOrgCode(orgCode);
                insureApplicant.insert();
            }
            return ResultUtil.data(insureApplicant);
        } catch (Exception e) {
            log.error("获取投保人异常:",e);
            throw new CustomException("获取投保人异常");
        }
    }

    private InsureApplicant getInsureApplicant(QyzxEntInfoM qyzxEntInfoM) {
        InsureApplicant insureApplicant;
        insureApplicant = new InsureApplicant();
        insureApplicant.setApplicantEName(qyzxEntInfoM.getName());
        insureApplicant.setApplicantEAddress(qyzxEntInfoM.getWorkAddress());
        insureApplicant.setApplicantEContacts(qyzxEntInfoM.getLinkMan());
        insureApplicant.setApplicantEPhone(qyzxEntInfoM.getPhone());/*这里为避免联系人为空,默认使用法人*/
        insureApplicant.setApplicantType("2");
        insureApplicant.setApplicantENoType("3646");
        insureApplicant.setApplicantENo(qyzxEntInfoM.getCreditCode());
        return insureApplicant;
    }

    @GetMapping("/getApplicant")
    @ApiOperation(value = "获取投保人--8小时", httpMethod = "GET", notes = "获取投保人")
    @Transactional(rollbackFor = Exception.class)
    public Result<Object> getApplicant(@CurrentUser UserBean userBean) {
        try {
            InsureApplicant insureApplicant = InsureApplicant.builder().build().selectOne(new QueryWrapper<InsureApplicant>().lambda().eq(InsureApplicant::getOrgCode, userBean.getOrgCode()));
            if (insureApplicant == null) {
                QyzxEntInfoM qyzxEntInfoM = QyzxEntInfoM.builder().id(userBean.getOrgCode()).build().selectById();
                insureApplicant = getInsureApplicant(qyzxEntInfoM);
                insureApplicant.setOrgCode(userBean.getOrgCode());
                insureApplicant.insert();
            }
            return ResultUtil.data(insureApplicant);
        } catch (Exception e) {
            log.error("获取投保人异常:",e);
            throw new CustomException("获取投保人异常");
        }
    }
    @GetMapping("/getProvince")
    @ApiOperation(value = "获取投保省市区", httpMethod = "GET", notes = "获取投保人")
    @Transactional(rollbackFor = Exception.class)
    public Result<Object> getRegion(){
        List<InsureProvince> nodeList= InsureProvince.builder().build().selectList(new QueryWrapper<InsureProvince>().lambda().isNotNull(InsureProvince::getId));
        // 配置
        TreeNodeConfig treeNodeConfig = new TreeNodeConfig();
        // 自定义属性名 都要默认值的
        treeNodeConfig.setIdKey("id");
        treeNodeConfig.setParentIdKey("parentId");
        // 最大递归深度
        treeNodeConfig.setDeep(4);

        List<Tree<String>> treeNodes = TreeUtil.build(nodeList, "0", treeNodeConfig, (treeNode, tree) -> {
            tree.setId(treeNode.getId().toString());
            tree.setParentId(treeNode.getPid().toString());
            tree.setName(treeNode.getName());
            tree.putExtra("value", treeNode.getValue());
        });

        return ResultUtil.data(treeNodes);
    }

}