/**
 *
 */
package com.sifang.modules.flowable.dao;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.sifang.modules.sunshine.SystemSetting.utils.StringUtil;
import com.sifang.modules.sys.common.persistence.BaseDao;
import com.sifang.modules.sys.common.persistence.Parameter;
import com.sifang.modules.sys.util.CrmUtils;
import com.sifang.modules.sys.util.LayuiPage;
import com.sifang.modules.sys.util.SqlBuilder;
import org.hibernate.SQLQuery;
import org.springframework.stereotype.Repository;
import com.sifang.modules.flowable.entity.ProcessDef;

import java.util.List;
import java.util.Map;

/**
 * @author Generate Tools
 * @version
 */
@Repository
public class ProcessDefDao extends BaseDao<ProcessDef> {


    public ProcessDef getById(String processId) {
        return getByHql("from ProcessDef d where WHERE d.id =:p1 ", new Parameter(processId));
    }


    public void processFormList(LayuiPage<Record> page, Map<String, String> map) {
        SqlBuilder sf = new SqlBuilder();
        sf.line(" SELECT l.id,l.process_def_id as processId,d.name,d.`description`,d.`status`,d.`create_by`,DATE_FORMAT(d.create_time,'%Y-%m-%d %H:%i:%s')AS createTime,d.status ");
        sf.line(" FROM `crm_process_def_latest` l LEFT JOIN `crm_process_def` d ON l.`process_def_id`=d.id WHERE d.status<>'2' ");
        getConditionMap(map, sf);
        sf.line(" ORDER BY d.create_time DESC  ");
        if (page != null) {
            sf.line(" limit " + page.getLimit() + " offset " + (page.getPage() - 1) * page.getLimit());
            Long count = getCount(map);
            page.setCount(count);
        }
        List<Record> list = Db.find(sf.build(), sf.params());
        for (Record r : list) {
            r.set("createName", CrmUtils.getUserXM(r.getStr("create_by")));
        }
        page.setData(list);
    }

    public long getCount(Map<String, String> map) {
        SqlBuilder sf = new SqlBuilder();
        sf.line(" SELECT COUNT(l.id) FROM `crm_process_def_latest` l LEFT JOIN `crm_process_def` d ON l.`process_def_id`=d.id WHERE d.status<>'2'  ");
        getConditionMap(map, sf);
        return Db.queryLong(sf.build(), sf.params());
    }

    public void getConditionMap(Map<String, String> map, SqlBuilder sf) {
        if (StringUtil.isNotBlank(map.get("name"))) {
            sf.like(" AND d.name LIKE ? ", map.get("name"));
        }
        if (StringUtil.isNotBlank(map.get("status"))) {  // 0：启动，1：禁用
            sf.line(" AND d.status = ? ", map.get("status"));
        }

    }

    // 启用/禁用/删除 流程
    public int changeProcessStatus(String processId, String type) {
        SQLQuery sqlQuery = getSession().createSQLQuery("UPDATE `crm_process_def` d SET d.status= :status WHERE d.id= :id  ");
        sqlQuery.setParameter("status", type);
        sqlQuery.setParameter("id", processId);
        int count = sqlQuery.executeUpdate();
        return count;
    }

    public Record getDefDetail(String processId) {
        String sql = " SELECT p.id as processId, p.name,p.form_id AS formId,p.description,p.status ,f.name AS formName FROM `crm_process_def` p LEFT JOIN `crm_process_form` f ON p.`form_id`=f.id WHERE p.id=? AND p.status<>'2' ";
        Record processDef = Db.findFirst(sql, processId);
        return processDef;
    }

    public List<Record> getForm() {
        String sql = " SELECT f.id,f.name FROM `crm_process_form` f WHERE f.id NOT IN ( SELECT d.form_id FROM`crm_process_def_latest` l LEFT JOIN `crm_process_def` d ON l.process_def_id = d.id WHERE d.status<>'2' ) ";
        List<Record> list = Db.find(sql);
        return list;
    }


    public List<Record> getAllRole(){
        String sql = " SELECT r.id,r.name FROM `crm_platform_employee_role` r WHERE r.status='0' and trader_id='1' ORDER BY sort ASC ";
        List<Record> list = Db.find(sql);
        return list;
    }

    public List<Record> getAllPlateEmp(){
        String sql="select id,name,user_id from crm_platform_employee e where e.status='0' order by e.department_id ";
        return Db.find(sql);
    }

}
