package com.jfinal.club.ws;

import com.jfinal.club.common.model.Docws;
import com.jfinal.club.common.model.Wsinfo;
import com.jfinal.club.util.Cons;
import com.jfinal.plugin.activerecord.Page;

/**
 * @ProjectName: jfinal-club
 * @Package: com.jfinal.club.ws
 * @ClassName: WsInfoService
 * @Author: Jzdsh
 * @Description: 文书服务层
 * @Date: 2020/4/14 15:05
 * @Version: 1.0
 */
public class WsInfoService {



    /**
     * 创建文书
     * @param userId
     * @return
     */
    public Wsinfo createWs(int userId) {
        //如果查询到文书 没有完成就用这个文书
        Wsinfo ws = Wsinfo.dao.findFirst("select * from wsinfo where userId = ? and status = 0", userId);
        if(ws != null){
            return ws;
        }
        //未查到 就创建一个新文书 保证有一个文书可用
        Wsinfo wsinfo = defaultAdd(userId);
        if(wsinfo != null){
            return wsinfo;
        }
        return null;
    }

    /**
     * 文书默认添加信息
     * @param userId
     * @return
     */
    public Wsinfo defaultAdd(int userId){
        try {
            Wsinfo wsinfo = new Wsinfo( );
            wsinfo.setUserId(userId);
            //是否请律师
            wsinfo.setLawyer("是");
            //律师费用默认300元
            wsinfo.setMoney(300D);
            wsinfo.setStatus(Cons.ZERO);
            wsinfo.save();
            return wsinfo;
        } catch (Exception e) {
            e.printStackTrace( );
        }
        return null;
    }

    /**
     * 我的文书
     * @param usersId
     * @return
     */
    public Page< Wsinfo > myws(int usersId,int pageNum,int pageSize) {

        try {
            return Wsinfo.dao.paginate(pageNum,pageSize,"select *"," from wsinfo where userId = ? and status = 1",usersId);
        } catch (Exception e) {
            e.printStackTrace( );
        }
        return null;
    }


    public Wsinfo getById(Integer wsid) {
        return  Wsinfo.dao.findById(wsid);
    }
}
