package com.sys.bus.sys.controller;


import com.alibaba.fastjson.JSONObject;
import com.jfinal.aop.Before;
import com.jfinal.aop.Enhancer;
import com.jfinal.core.Controller;
import com.jfinal.ext.interceptor.POST;
import com.jfinal.plugin.activerecord.Page;
import com.jfplugin.mail.MailKit;
import com.sys.bus.sys.model.User;
import com.sys.bus.sys.service.UserService;
import com.sys.core.model.DataBean;
import com.sys.core.utils.Tool;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.UUID;

public class UserController extends Controller{
	Log log = LogFactory.getLog(this.getClass());
	static UserService service = Enhancer.enhance(UserService.class);//需要事务处理的service，必须使用增强方法
	public void index(){
		//AutoSql.show();
		this.renderJsp("user.jsp");//加斜杠不加斜杠有区别，不加到配置的路径下去找视图；加则到根目录下找视图
	}
	public void index2(){
	}
	@Before(value= {POST.class})
	public void save(){
		User user=new User();
		//user.set("id", UUID.randomUUID().toString());//users_sequence.nextval
		DataBean dBean = new DataBean();
		dBean.setCode(0);
		dBean.setMsg("Ok");
		user.set("id", UUID.randomUUID().toString());
		//user.set("name", "张三");
		user.setName("zhangsan");
		user.setCF_COLORE_TYPE("B");
		user.set("age", 18);
		service.save(user);
		this.renderJson("data",dBean);
	}
	public void saveForm() {
		String name = this.getPara("name");
		String age = this.getPara("age");
		User user = new User();
		user.set("name", name).set("age", age);
		service.save(user);
		this.render("ok.jsp");
		//表单传递
		//getModel(User.class).save();

	}
	public void update() {
		//getModel(User.class).update();
	}

	//关联查询
	@Before(POST.class)
	public void list(){
		String para = "";
		DataBean dBean = new DataBean();
		dBean.setCode(0);
		dBean.setMsg("Ok");
		dBean.setData(service.list(para));
		this.renderJson("json",dBean);
	}
	//分页查询
	@Before(POST.class)
	public void page() {
		Page<User> pageData = service.page(1, 5);
		DataBean dBean = new DataBean();
		dBean.setCode(0);
		dBean.setMsg("Ok");
		List<Page<User>> list = new ArrayList<Page<User>>();
		list.add(pageData);
		dBean.setData(list);
		this.renderJson("json",dBean);
	}
	public void test() {
		System.out.println(1/0);
		this.render("/test.jsp");
	}
	//把前端js json对象转换成java json对象
	@Before(POST.class)
	public void jsonParameter() {
		JSONObject jsonObj = null;
		try {
			jsonObj = Tool.getRequestObject(this.getRequest());
		} catch (Exception e) {
			e.printStackTrace();
		}
		this.renderJson("json",jsonObj);
	}
	//查询对象明细
	@Before(POST.class)
	public void detail() {
		String id= this.getPara("id");
		//getParaToInt();
		//getParaToInt(0, 1);
		DataBean dBean = new DataBean();
		dBean.setCode(0);
		dBean.setMsg("Ok");
		User user = service.findById(id);

		List<User> list = new ArrayList<User>();
		list.add(user);
		dBean.setData(list);
		this.renderJson("json",dBean);
		//this.setAttr("user", user);
	}

	public void mail() {
		DataBean dBean = new DataBean();
		dBean.setCode(0);
		dBean.setMsg("Ok");
		try {
			MailKit.send("tangzy115049@hanslaser.com",Arrays.asList("tangzy115049@hanslaser.com","tangzy115049@hanslaser.com"), "标题", "<a href=\"\" style=\"color:red\">邮件内容</a>");
		}catch(Exception e) {
			dBean.setCode(-1);
			dBean.setMsg("Error");
			log.error("",e);
		}
		this.renderJson("json",dBean);
	}

}
