package com.engine.recruit.service.impl;

import com.engine.core.impl.Service;
import com.engine.recruit.entity.record.ApplicantRecordPo;
import com.engine.recruit.enums.RecordOperateEnum;
import com.engine.recruit.exception.CustomizeRunTimeException;
import com.engine.recruit.service.OfferService;
import org.apache.commons.lang3.StringUtils;
import weaver.common.DateUtil;
import weaver.conn.RecordSet;
import weaver.general.Util;

import java.util.HashMap;
import java.util.Map;

/**
 * @author:dxfeng
 * @createTime: 2023/10/30
 * @version: 1.0
 */
public class OfferServiceImpl extends Service implements OfferService {
    @Override
    public Map<String, Object> getOfferMobileLink(Map<String, Object> param) {
        return null;
    }

    @Override
    public Map<String, Object> getOfferContent(Map<String, Object> param) {
        Map<String, Object> returnMap = new HashMap<>();
        String billId = Util.null2String(param.get("billid"));
        String email = Util.null2String(param.get("dzyx"));
        RecordSet rs = new RecordSet();
        String offerContent = "";
        String sxrq = "";
        rs.executeQuery("select tznr,sxrq from uf_jcl_offer where id = ? and dzyx = ?", billId, email);
        if (rs.next()) {
            offerContent = rs.getString("tznr");
            sxrq = rs.getString("sxrq");
        }
        // 判断当前offer是否失效
        String currentDate = DateUtil.getCurrentDate();
        int compDate = DateUtil.compDate(currentDate, sxrq);
        // 当前日期>失效日期，更新状态为逾期未回复
        if (compDate < 0) {
            throw new CustomizeRunTimeException("offer已失效");
        }
        if (StringUtils.isAnyBlank(billId, email, offerContent)) {
            throw new CustomizeRunTimeException("非法访问");
        }
        returnMap.put("content", offerContent);
        return returnMap;
    }

    @Override
    public Map<String, Object> updateOfferStatus(Map<String, Object> param) {
        String status = Util.null2String(param.get("status"));
        String email = Util.null2String(param.get("dzyx"));
        String billId = Util.null2String(param.get("billid"));
        if (StringUtils.isAnyBlank(billId, email, status) || !("2".equals(status) || "3".equals(status))) {
            throw new CustomizeRunTimeException("非法操作");
        }
        RecordSet rs = new RecordSet();
        // 判断是否已提交
        rs.executeQuery("select zt from uf_jcl_offer where id = ? and dzyx = ? ", billId, email);
        if (rs.next()) {
            String zt = rs.getString("zt");
            if ("2".equals(zt) || "3".equals(zt)) {
                throw new CustomizeRunTimeException("此反馈已提交，请勿重复提交");
            }
        }

        // 更新反馈状态、反馈时间
        rs.executeUpdate("update uf_jcl_offer set zt = ?, fksj = ? where id = ? and dzyx = ?", status, DateUtil.getDateTime(), billId, email);

        // 记录应聘过程
        ApplicantRecordPo recordPo = ApplicantRecordPo.builder()
                .billId(String.valueOf(billId))
                .operateTime(DateUtil.getDateTime())
                .user(user)
                .recordOperateType(RecordOperateEnum.OFFER_FEEDBACK)
                .build();
        recordPo.execute();

        return null;
    }

}
