package com.sys.bus.jx.service;

import com.sys.bus.hr.model.Person;
import com.sys.bus.model.Entry;
import com.sys.bus.model.Kpi;
import com.sys.bus.model.Wflog;
import com.sys.core.utils.Tool;

import java.util.List;

public class WflogService {
    /**
     *
     * @param operator 操作着
     * @param wfname  日志名称
     * @param type    0跳转  1绩效经理启动考核指标  2 绩效经理启动考核评分   3 自评 4评分人   5绩效经理KPI  6绩效经理考核评分  7 自填kpi 8 领导审核kpi
     * @param cycleid
     * @param entryid
     */
    public  void addWfLog(Person operator, String wfname, String type, String cycleid, String entryid, String entryno,  String logResult, String logInfo){
        Wflog wflog = new Wflog();
        wflog.setWflogId(Tool.uuid());
        wflog.setWflogName(wfname);
        wflog.setWflogType(type);
        wflog.setWflogCycleid(cycleid);
        wflog.setWflogEntryno(entryno);
        wflog.setWflogEntryid(entryid);
        wflog.setWflogResult(logResult);
        wflog.setWflogInfo(logInfo);
        wflog.setWflogDate(Tool.getCurrTime());
        wflog.setWflogCreatorid(operator==null?"":operator.getId());
        wflog.setWflogCreatorname(operator==null?"":operator.getName());
        wflog.save();
    }

    /**
     *
     * @param entryId
     */
    public  List<Wflog> getWfLog(String entryId){
        Entry entry = Entry.dao.findById(entryId);
        String entryNo = entry.getEntryNo();
        return Wflog.dao.find("select * from ct_jx_wflog where wflog_entryid =? or (wflog_entryno=? and wflog_entryid is null) order by wflog_date desc",entryId,entryNo);
    }

    /**
     *
     * @param entryId
     */
    public  boolean  existAppType(String entryId,String appType){
        Entry entry = Entry.dao.findById(entryId);
        List<Wflog> wflogs = Wflog.dao.find("select * from ct_jx_wflog where wflog_entryid =? and wflog_type=?", entryId, appType);
        return wflogs!=null && wflogs.size()>0;
    }

    /**
     *
     * @param entry
     * @param type
     * type = 4 评分人打回日志
     * type = 6 绩效经理打回评分结果
     */
    public  void setBackWfLog(Entry entry, String type) {
        if(!"3".equals(entry.getEntryWfstatus()))return;
        String sql = "select * from ct_jx_wflog  where wflog_type = '"+type+"'  and  wflog_entryid = ? order by wflog_date desc";
        List<Wflog> wflogs = Wflog.dao.find(sql,entry.getEntryId());
       if(wflogs.size() ==0)return;
        Wflog wlog = wflogs.get(0);
        if("2".equals(wlog.getWflogResult())){
            entry.put("backReason",wlog.getWflogInfo() + "("+wlog.getWflogCreatorname()+"  /  "+wlog.getWflogDate()+")");
        }



    }

    /**
     *
     * @param jxEntry
     * type = 5 绩效经理打回kpi
     * type = 8 领导打回kpi
     */
    public void setBackWfLog(Entry jxEntry){
        String entryId = jxEntry.getEntryId();
        String sql = "select * from ct_jx_wflog  where (wflog_type = '5' or wflog_type = '8')  and wflog_result = '2' and  wflog_entryid = ?  order by wflog_date desc";
        List<Wflog> wflogs = Wflog.dao.find(sql,entryId);
        if(wflogs.size()>0){//只显示最新的一条打回原因
            jxEntry.put("backReason",wflogs.get(0).getWflogInfo() + "(操作人:"+wflogs.get(0).getWflogCreatorname()+";操作时间:"+wflogs.get(0).getWflogDate()+")");
        }
    }
    public void setApplyWfLog(Entry jxEntry){
        String entryId = jxEntry.getEntryId();
        String sql = "select * from ct_jx_wflog  where (wflog_type = '71')  and wflog_result = '3' and  wflog_entryid = ?  order by wflog_date desc";
        List<Wflog> wflogs = Wflog.dao.find(sql,entryId);
        if(wflogs.size()>0){//只显示最新的一条打回原因
            jxEntry.put("applyReason",wflogs.get(0).getWflogInfo() + "(申请时间:"+wflogs.get(0).getWflogDate()+")");
        }
    }

}
