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

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.sifang.modules.sys.common.persistence.BaseDao;
import com.sifang.modules.sys.util.LayuiPage;
import com.sifang.modules.sys.util.SqlBuilder;
import com.sifang.modules.taskdispatch.entity.CrmReviewGroup;
import org.hibernate.Query;
import org.springframework.stereotype.Repository;

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

/**
 * 保险信息DAO接口
 * @author gaols
 */
@Repository
public class CrmReviewGroupDao extends BaseDao<CrmReviewGroup> {

    public void listData(LayuiPage<Record> page, Map<String, String> params) {
        SqlBuilder sb = new SqlBuilder();
        sb.line("select g.id,g.name,pe.name as leader_name,g.work_time_st1 as st1,");
        sb.line("g.work_time_end1 as end1,g.work_time_st2 as st2,g.work_time_end2 as end2,g.partner_id");
        sb.line("from crm_review_group as g");
        sb.line("left join crm_review_group_member as m on m.group_id=g.id and m.leadership='1' and m.status='0'");
        sb.line("left join crm_platform_employee as pe on pe.id=m.employee_id and pe.status='0'");
        sb.line("where g.status='0' and g.partner_id=?", params.get("partnerId"));
        sb.line("order by g.create_time desc");
        sb.limit(page.getPage(), page.getLimit());
        List<Record> records = Db.find(sb.build(), sb.params());
        page.setData(records);
    }

    @SuppressWarnings("unchecked")
    public List<CrmReviewGroup> findAll() {
        Query query = getSession().createQuery("from CrmReviewGroup where status='0'");
        return query.list();
    }

    public void listDataCount(LayuiPage<Record> page, Map<String, String> params) {
        SqlBuilder sb = new SqlBuilder();
        sb.line("select count(*)");
        sb.line("from crm_review_group as g");
        sb.line("left join crm_review_group_member as m on m.group_id=g.id and m.leadership='1' and m.status='0'");
        sb.line("left join crm_platform_employee as pe on pe.id=m.employee_id and pe.status='0'");
        sb.line("where g.status='0'");
        page.setCount(Db.queryLong(sb.build(), sb.params()));
    }

    public Record getById(String id) {
        return Db.findFirst("select * from crm_review_group where id=? for update", id);
    }

    public Record getByPartnerIdAndType(String partnerId, String type) {
        SqlBuilder sb = new SqlBuilder()
                .line("select * from crm_review_group where partner_id=? and `type`= ? and status='0'", partnerId, type);
        return Db.findFirst(sb.build(), sb.params());
    }

    public void updateWorkTime(String leaderEmpId,
                               String worktimeStart1, String worktimeEnd1,
                               String worktimeStart2, String worktimeEnd2,
                               String groupId) {
        SqlBuilder sb = new SqlBuilder()
                .line("update crm_review_group set leader_emp_id=?,work_time_st1=?,work_time_end1=?,work_time_st2=?,work_time_end2=? where id=? ",
                        leaderEmpId, worktimeStart1,worktimeEnd1, worktimeStart2,worktimeEnd2, groupId);
        Db.update(sb.build(), sb.params());
    }

    public Record findByUserId(String userId) {
        SqlBuilder sb = new SqlBuilder()
                .line("select g.* from crm_review_group as g")
                .line("inner join crm_review_group_member as m on m.group_id=g.id and m.user_id=? and m.status='0'", userId)
                .line("where g.status='0'");
        return Db.findFirst(sb.build(), sb.params());
    }

    public Record getGroupLeader(String groupId) {
        SqlBuilder sb = new SqlBuilder()
            .line("select m.id,e.name from crm_review_group_member as m")
            .line("inner join crm_platform_employee as e on e.id=m.employee_id and e.status='0'")
            .line("where m.leadership='1' and m.status='0' and m.group_id=?", groupId);
        return Db.findFirst(sb.build(), sb.params());
    }
}
