package jnpf.personnels.controller.web.roster;

import jnpf.base.ActionResult;
import jnpf.base.UserInfo;
import jnpf.config.ConfigValueUtil;
import jnpf.database.util.TenantDataSourceUtil;
import jnpf.exception.LoginException;
import jnpf.model.personnels.dto.contractinfo.ContactStatusInfo;
import jnpf.personnels.FtbPersonnelsContactInfoManagerApi;
import jnpf.personnels.service.FtbPersonnelsContactInfoService;
import jnpf.util.NoDataSourceBind;
import jnpf.util.StringUtil;
import jnpf.util.UserProvider;
import jnpf.util.data.DataSourceContextHolder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;

/**
 * web电子合同同步模块
 *
 * @author xxxxx
 */
@RestController
@Slf4j
@RequestMapping("/web/personnelsContactInfo")
public class FtbPersonnelsContactInfoController implements FtbPersonnelsContactInfoManagerApi {
    @Autowired
    private FtbPersonnelsContactInfoService contactInfoService;

    @Autowired
    private ConfigValueUtil configValueUtil;


    /**
     * 同步合同信息
     *
     * @param info
     * @return
     */
    @PostMapping("/syncContactInfo")
    @NoDataSourceBind
    public ActionResult<Boolean> syncContactInfo(@RequestBody ContactStatusInfo info) {

        switchTenantId(info.getTenantCode());
        contactInfoService.syncContactInfo(info);
        return ActionResult.success("成功", true);
    }



    private void switchTenantId(String tenantId) {
        // 判断是否为多租户
        if (configValueUtil.isMultiTenancy()) {
            // 判断是不是从外面直接请求
            if (StringUtil.isNotEmpty(tenantId)) {
                //切换成租户库
                try {
                    TenantDataSourceUtil.switchTenant(tenantId);
                } catch (LoginException e) {
                    throw new RuntimeException("切换租户失败");
                }
            } else {
                UserInfo userInfo = UserProvider.getUser();
                Assert.notNull(userInfo.getUserId(), "缺少租户信息");
                DataSourceContextHolder.setDatasource(userInfo.getTenantId(), userInfo.getTenantDbConnectionString(), userInfo.isAssignDataSource());
            }
        }
    }

    /**
     * 查询入职未发起的合同，并发送IM消息
     *
     * @param tenantId
     * @return
     */
    @GetMapping("/checkNotSendContactSign")
    @NoDataSourceBind
    public ActionResult<Boolean> checkNotSendContactSign(@RequestParam("tenantId") String tenantId) {

        switchTenantId(tenantId);
        contactInfoService.checkNotSendContactSign(tenantId);
        return ActionResult.success("成功", true);
    }

    /**
     * 下载合同信息
     * @param userId
     * @param response
     */
    @PostMapping("/downLoad/{userId}")
    public void downLoad(@PathVariable("userId") String userId, HttpServletResponse response) {
        contactInfoService.FileListZip("合同详情", userId, response);
    }

}
