package com.jeelowcode.module.biz.controller;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.jeelowcode.core.framework.controller.BaseController;
import com.jeelowcode.core.framework.service.IFormService;
import com.jeelowcode.framework.exception.JeeLowCodeException;
import com.jeelowcode.framework.global.JeeLowCodeBaseConstant;
import com.jeelowcode.framework.tenant.annotation.JeeLowCodeTenantIgnore;
import com.jeelowcode.framework.utils.model.ResultDataModel;
import com.jeelowcode.framework.utils.tool.CollectionUtil;
import com.jeelowcode.tool.framework.common.pojo.CommonResult;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;
import java.util.regex.Pattern;

import static com.jeelowcode.tool.framework.common.pojo.CommonResult.success;

@JeeLowCodeTenantIgnore
@Tag(name = "低代码框架 - 自定义外协人员接口")
@RestController
@AllArgsConstructor
@RequestMapping(JeeLowCodeBaseConstant.REQUEST_URL_START + "/outsidePerson")
public class OutSidePersonController extends BaseController {

    @Autowired
    private IFormService dbFormService;

    @GetMapping({"/importOutside"})
    @ApiOperationSupport(order = 5)
    @Operation(summary = "引入外协人员")
    public CommonResult<String> importOutside(String tableId,String ids) {
        String[] idArr = ids.split(Pattern.quote(","));
        if (Objects.isNull(idArr) || idArr.length == 0) {
            throw new JeeLowCodeException("缺少必要参数");
        }

        String tableName="lc_outside_person";
        Long dbFormId = dbFormService.getDbFormIdByTableName(tableName);
        List<JSONObject> licenses = new ArrayList<JSONObject>();
        for (int i = 0; i < idArr.length; i++) {
            ResultDataModel resultDataModel = super.getDataDetail(dbFormId, Long.valueOf(idArr[i]), new HashMap<String, Object>());
            if (CollectionUtil.isNotEmpty(resultDataModel.getRecords())) {
                HashMap<String, Object> detail = (HashMap<String, Object>) resultDataModel.getRecords().get(0);

                HashMap<String, Object> newLicense = new HashMap<>();
                newLicense.put("billNo", "WX" + System.currentTimeMillis());
                newLicense.put("outsideId", detail.get("id"));
                newLicense.put("workPlace", detail.get("workPlaceName"));
                newLicense.put("workPlaceId", detail.get("workPlaceId"));
                newLicense.put("demandId", detail.get("demandId"));
                newLicense.put("demandName", detail.get("demandName"));
                newLicense.put("supplierId", detail.get("supplierId"));
                newLicense.put("supplierName", detail.get("supplierName"));
                newLicense.put("personName", detail.get("personName"));
                newLicense.put("personSex", detail.get("personSex"));
                newLicense.put("personNo", detail.get("cardNo"));
                newLicense.put("mobile", detail.get("mobile"));
                licenses.add(JSONUtil.parseObj(newLicense));
            }

        }
        super.addJsonData( Long.valueOf(tableId), licenses);
        return success("引入成功");
    }
}
