package cn.hmsoft.frame.control;

import java.io.File;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import cn.hmsoft.application.web.Ajax;
import cn.hmsoft.frame.constants.FrameResIdConstants;
import cn.hmsoft.frame.data.dao.FrameOptrDao;
import cn.hmsoft.frame.data.dao.FrameRoleDao;
import cn.hmsoft.frame.data.model.FrameDept;
import cn.hmsoft.frame.data.model.FrameOptr;
import cn.hmsoft.frame.data.model.FrameRole;
import cn.hmsoft.frame.service.FrameOptrService;
import cn.hmsoft.frame.util.FrameDictUtil;
import cn.hmsoft.frame.util.FrameParamUtil;
import cn.hmsoft.helper.FileHelper;
import cn.hmsoft.helper.ImageHelper;
import cn.hmsoft.jdbc.entity.Pager;

@RestController
// @Api(value = "操作员管理")
public class FrameOptrControl extends FrameControl {

	@Autowired
	private FrameOptrService sOptr;
	@Autowired
	private FrameOptrDao daoFrameOptr;
	@Autowired
	private FrameRoleDao daoRole;

	@RequestMapping("frame/optr/page")
	public Ajax page(Integer role_id, String query, Integer limit, Integer start, String order, String type) {
		this.authOptrRole(FrameResIdConstants.Optr_Page);
		return new Ajax(this.daoFrameOptr.pageOptr(this.getFrameOptr().getOptr_id(), start, limit, query,
				getQueryOrder(order, type)));
	}

	@RequestMapping("frame/optr/all")
	public Ajax all() {
		this.authOptrRole(FrameResIdConstants.Optr_Page);
		return new Ajax(new Pager(this.daoFrameOptr.all()));
	}

	@RequestMapping("frame/optr/find")
	public Ajax find(Integer optr_id) {
		this.authOptrRole(FrameResIdConstants.Optr_Manage);
		return new Ajax(this.daoFrameOptr.find(optr_id));
	}

	@RequestMapping("frame/optr/add")
	public Ajax add(String login_name, FrameOptr optr) {
		this.authOptrRole(FrameResIdConstants.Optr_Manage);
		this.sOptr.addOptr(optr, this.getFrameOptr());
		return new Ajax(optr);
	}

	@RequestMapping("frame/optr/edit")
	public Ajax edit(FrameOptr optr) {
		if (optr.getOptr_id() - this.getFrameOptr().getOptr_id() != 0)
			this.authOptrRole(FrameResIdConstants.Optr_Manage);
		this.sOptr.editOptr(optr, this.getFrameOptr());
		return new Ajax();
	}

	@RequestMapping("frame/optr/delete")
	public Ajax delete(int optr_id) {
		this.authOptrRole(FrameResIdConstants.Optr_Manage);
		this.sOptr.deleteOptr(optr_id, this.getFrameOptr());
		return new Ajax();
	}

	@RequestMapping("frame/optr/role/dist")
	public Ajax distRole(int optr_id, int role_id) {
		this.authOptrRole(FrameResIdConstants.Optr_Manage);
		this.sOptr.distRole(optr_id, role_id, this.getFrameOptr());
		return new Ajax();
	}

	@RequestMapping("frame/optr/role/recover")
	public Ajax recoverRole(int optr_id, int role_id) {
		this.authOptrRole(FrameResIdConstants.Optr_Manage);
		this.sOptr.recoverRole(optr_id, role_id, this.getFrameOptr());
		return new Ajax();
	}

	@RequestMapping("frame/optr/passwd")
	public Ajax passwd(int optr_id, String old_password, String new_pass) {
		if (this.getFrameOptr().getOptr_id() - optr_id != 0)
			this.authOptrRole(FrameResIdConstants.Optr_Manage);
		this.sOptr.modifyPasswd(optr_id, new_pass, old_password, getFrameOptr());
		return new Ajax();
	}

	@RequestMapping("frame/optr/image/upload")
	public Ajax uploadImage(MultipartFile file) {
		// 保存到临时文件中
		File imageTemp = FileHelper.fromUpload(file);
		// 获取文件类型
		String imageType = ImageHelper.getImageType(imageTemp);
		File f = FileHelper.createWebTempFile(imageType);
		ImageHelper.zipImage(FileHelper.fromUpload(file), 128, 128, f.getPath());
		String v = "data:image/" + imageType + ";base64," + ImageHelper.ImageToBase64ByLocal(f.getPath());
		imageTemp.delete();
		this.sOptr.uploadImage(this.getFrameOptr().getOptr_id(), v);
		return new Ajax();
	}

	@RequestMapping("frame/optr/init")
	public Ajax init() {
		Map<String, Object> map = new HashMap<String, Object>();
		// 字典信息
		map.put("FrameDictArray", FrameDictUtil.allDict());
		// 服务器时间
		map.put("FrameCurrentTime", LocalDateTime.now());
		// 参数
		map.put("FrameParamArray", FrameParamUtil.FrameParamArray);
		FrameOptr optr = this.getFrameOptr();
		FrameOptr optrFront = new FrameOptr();
		BeanUtils.copyProperties(optr, optrFront);
		optrFront.setLogin_pass("******");
		// optrFront.setResMap(null);
		// 用户信息
		map.put("FrameOptr", optrFront);
		// 部门信息
		map.put("FrameDept", this.daoFrameOptr.find(FrameDept.class, "dept_id", optr.getOptr_dept()));
		// 获取资源信息
		map.put("FrameResArray", this.sOptr.fetchOptrRes(optr.getOptr_id()));
		map.put("FrameSessionId", this.getSession().getId());
		map.put("FrameServletName", this.getRequest().getContextPath().replace("/", ""));
		//用户所属角色
		FrameRole role = this.daoRole.listRole(optr.getOptr_id());
		map.put("HomePage", role == null ? "" :role.getHome_page());
		return new Ajax(map);
	}

}
