package cn.hmsoft.skill.data.dao.std;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import org.springframework.stereotype.Repository;
import cn.hmsoft.jdbc.core.PlatformDaoSupport;
import cn.hmsoft.skill.data.model.std.StdMaterial;

@Repository
public class StdMaterialDao extends PlatformDaoSupport<StdMaterial> {
	private final static String FIND_MATERIAL = "select * from std_material where std_id=? and material_type=?";

	/*********************
	 * 根据材料类型查找记录
	 * 
	 */
	public StdMaterial findMaterial(int std_id, String material_type) {
		return this.findBySql(FIND_MATERIAL, std_id, material_type);
	}

	private final static String MAP_ALL_MATERIAL = "select m.*,s.material_id,s.material_file,s.upload_time,s.verify_status,s.verify_time,s.reupload_status,s.verify_msg,s.verify_optr,s.verify_auto from cf_enrol_material m left join (select * from std_material where std_id=?) s on s.material_type = m.material_type order by m.material_order";

	/*********************
	 * 找出所有需要的材料，如果material_id为空，说明没上传
	 *
	 */
	public List<Map<String, Object>> listAllMaterial(int std_id) {
		return this.listMapBySql(MAP_ALL_MATERIAL, std_id);
	}

	private final static String REUPLOAD_MATERIAL = "update std_material set material_file=?,reupload_status='Active',verify_status = null,verify_msg=null,verify_time=null where material_id=?";

	/************************
	 * 重新上传了，需要覆盖审核信息
	 */
	public void reuploadMaterial(StdMaterial material) {
		if (material.getMaterial_id() != null)
			this.updateBySql(REUPLOAD_MATERIAL, material.getMaterial_file(),material.getMaterial_id());

	}

	private final static String REFUSE_MATERIAL = "update std_material set verify_time=?,reupload_status='InActive',verify_status = 'InActive',verify_msg=?,verify_optr=? where material_id=?";

	/************************
	 * 审核失败
	 */
	public void refuseMaterial(int material_id, String verify_msg, int optr_id) {
		this.updateBySql(REFUSE_MATERIAL, LocalDateTime.now(),verify_msg, optr_id, material_id);
	}
	
	private final static String ACCPET_MATERIAL = "update std_material set verify_time=?,verify_status = 'Active',verify_msg=null,verify_optr=? where material_id=?";

	/************************
	 * 审核失败
	 */
	public void acceptMaterial(int material_id,  int optr_id) {
		this.updateBySql(ACCPET_MATERIAL, LocalDateTime.now(),optr_id, material_id);
	}
}
