package cn.com.qmth.stmms.biz.basic.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.com.qmth.stmms.biz.basic.dao.PrivilegeDao;
import cn.com.qmth.stmms.biz.basic.dao.RoleInfoDao;
import cn.com.qmth.stmms.biz.basic.model.Privilege;
import cn.com.qmth.stmms.biz.basic.model.RoleInfo;
import cn.com.qmth.stmms.biz.basic.service.PrivilegeService;
import cn.com.qmth.stmms.biz.common.BaseQueryService;
import cn.com.qmth.stmms.common.enums.PrivilegeType;
import cn.com.qmth.stmms.common.enums.Role;

@Service("privilegeService")
public class PrivilegeServiceImpl extends BaseQueryService<Privilege> implements PrivilegeService {
	private static List<Privilege> privileges = null;
	private static Object lock = new Object();
	
	@PersistenceContext
	private EntityManager entityManager;
	@Autowired
	private PrivilegeDao privilegeDao;
	
	@Autowired
	private RoleInfoDao roleInfoDao;

	@Override
	public List<Privilege> getMenuPrivileges() {
		if (privileges == null) {
			synchronized (lock) {
				if (privileges == null) {
					privileges = privilegeDao.findByPrivilegeTypeOrderByLevelAscSeqAsc(PrivilegeType.MENU);
					if (privileges == null) {
						privileges = new ArrayList<>();
					}
				}
			}
		}
		return privileges;
	}

	@Transactional
	@Override
	public void initRoleAndPrivilege(Integer schoolId) {
		List<RoleInfo> ris=new ArrayList<>();
		ris.add(new RoleInfo(schoolId,Role.SYS_ADMIN, Role.SYS_ADMIN.getName(), 1));
		ris.add(new RoleInfo(schoolId,Role.SCHOOL_ADMIN, Role.SCHOOL_ADMIN.getName(), 2));
		ris.add(new RoleInfo(schoolId,Role.SCANNER, Role.SCANNER.getName(), 3));
		ris.add(new RoleInfo(schoolId,Role.SUBJECT_HEADER, Role.SUBJECT_HEADER.getName(), 4));
		ris.add(new RoleInfo(schoolId,Role.MARKER, Role.MARKER.getName(), 5));
		ris.add(new RoleInfo(schoolId,Role.SCHOOL_VIEWER, Role.SCHOOL_VIEWER.getName(), 6));
		ris.add(new RoleInfo(schoolId,Role.SCHOOL_DEV, Role.SCHOOL_DEV.getName(), 7));
		ris.add(new RoleInfo(schoolId,Role.INSPECTOR, Role.INSPECTOR.getName(), 8));
		ris.add(new RoleInfo(schoolId,Role.SCAN_ADMIN, Role.SCAN_ADMIN.getName(), 9));
		ris.add(new RoleInfo(schoolId,Role.COLLEGE_ADMIN, Role.COLLEGE_ADMIN.getName(), 10));
		roleInfoDao.save(ris);
		initSchoolAdminPrivilege(schoolId);
		initSubjectHeaderPrivilege(schoolId);
		initCollegeAdminPrivilege(schoolId);
		initInspectorPrivilege(schoolId);
		initSchoolViewerPrivilege(schoolId);
	}
	
	private void initSchoolAdminPrivilege(Integer schoolId) {
		StringBuilder sql = new StringBuilder();
		sql.append(" INSERT INTO b_role_privilege (`school_id`, `role_code`, `privilege_code`,`enable`) ");
		sql.append(" SELECT s.id,'SCHOOL_ADMIN',p.`code`,1 from b_privilege p join b_school s ");
		sql.append(" where s.id="+schoolId);
		Query query = entityManager.createNativeQuery(sql.toString());
		query.executeUpdate();
	}
	
	private void initSubjectHeaderPrivilege(Integer schoolId) {
		StringBuilder sql = new StringBuilder();
		sql.append("INSERT INTO b_role_privilege (`school_id`, `role_code`, `privilege_code`,`enable`)");
		sql.append(" SELECT s.id,'SUBJECT_HEADER',p.`code`,1 from b_privilege p join b_school s   ");
		sql.append(" where p.`code` in('exam_mark','exam_mark-mark','exam_mark-group','exam_mark-marker','exam_mark-trial','exam_mark-library'");;
		sql.append(" ,'exam_mark-library-inspect','exam_mark-library-reject','exam_mark-arbitrate','exam_mark-quality','exam_reject_list'");
		sql.append(" ,'exam_reject_list-paper','exam_reject_list-history','exam_problem_history','exam_problem_history-reset'");
		sql.append(" ,'exam_problem_history-batch_reset'");
		sql.append(" ,'exam_inspected_info','exam_inspected_info-list','exam_inspected_info-next_round'");
		sql.append(" ,'exam_score','exam_score-export','exam_report_subject','exam_report_subject_range')");
		sql.append(" and s.id="+schoolId);
		Query query = entityManager.createNativeQuery(sql.toString());
		query.executeUpdate();
	}
	private void initCollegeAdminPrivilege(Integer schoolId) {
		StringBuilder sql = new StringBuilder();
		sql.append("INSERT INTO b_role_privilege (`school_id`, `role_code`, `privilege_code`,`enable`)");
		sql.append(" SELECT s.id,'COLLEGE_ADMIN',p.`code`,1 from b_privilege p join b_school s   ");
		sql.append(" where p.`code` in('exam_mark','exam_mark-mark','exam_mark-group','exam_mark-marker','exam_mark-trial','exam_mark-library'");;
		sql.append(" ,'exam_mark-library-inspect','exam_mark-library-reject','exam_mark-arbitrate','exam_mark-quality','exam_reject_list'");
		sql.append(" ,'exam_reject_list-paper','exam_reject_list-history','exam_problem_history','exam_problem_history-reset'");
		sql.append(" ,'exam_problem_history-batch_reset'");
		sql.append(" ,'exam_inspected_info','exam_inspected_info-list','exam_inspected_info-next_round'");
		sql.append(" ,'exam_score','exam_score-export','exam_report_subject','exam_report_subject_range')");
		sql.append(" and s.id="+schoolId);
		Query query = entityManager.createNativeQuery(sql.toString());
		query.executeUpdate();
	}
	private void initInspectorPrivilege(Integer schoolId) {
		StringBuilder sql = new StringBuilder();
		sql.append(" INSERT INTO b_role_privilege (`school_id`, `role_code`, `privilege_code`,`enable`)");
		sql.append(" SELECT s.id,'INSPECTOR',p.`code`,1 from b_privilege p join b_school s   ");
		sql.append(" where p.`code` in('exam_inspected_info','exam_inspected_info-list','exam_inspected_info-next_round')");
		sql.append(" and s.id="+schoolId);
		Query query = entityManager.createNativeQuery(sql.toString());
		query.executeUpdate();
	}
	private void initSchoolViewerPrivilege(Integer schoolId) {
		StringBuilder sql = new StringBuilder();
		sql.append(" INSERT INTO b_role_privilege (`school_id`, `role_code`, `privilege_code`,`enable`)");
		sql.append(" SELECT s.id,'SCHOOL_VIEWER',p.`code`,1 from b_privilege p join b_school s   ");
		sql.append(" where p.`code` in('exam_score','exam_report_subject','operation_log')");
		sql.append(" and s.id="+schoolId);
		Query query = entityManager.createNativeQuery(sql.toString());
		query.executeUpdate();
	}
	
	@Override
	public List<Privilege> getPagePrivilegesByParentCode(String parentCode) {
		return privilegeDao.findByPrivilegeTypeAndParentCodeOrderBySeqAsc(PrivilegeType.PAGE,parentCode);
	}
}
