package com.jfinal.club.ws;

import com.jfinal.club.common.model.Wsinfo;
import com.jfinal.club.common.model.Wsman;
import com.jfinal.club.util.Cons;
import com.jfinal.plugin.activerecord.Db;

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

/**
 * @ProjectName: jfinal-club
 * @Package: com.jfinal.club.ws
 * @ClassName: WsmanService
 * @Author: Jzdsh
 * @Description: 文书原告被告信息
 * @Date: 2020/4/14 15:41
 * @Version: 1.0
 */
public class WsmanService {


    /**
     * 根据文书查询原告被告信息
     *
     * @param wsId
     * @param ygby 原告被告标识
     * @return
     */
    public List< Wsman > listByWsIdYgbg(Integer wsId, int ygby) {
        try {
            List< Wsman > wsmanList = Wsman.dao.find("select * from wsman where wsId = ? and ygbg = ? ", wsId, ygby);
            if (wsmanList != null && wsmanList.size( ) > 0) {
                return wsmanList;
            }
            List< Wsman > wsmen = new ArrayList<>( );
            //传来一个 原告id 返回一个原告,一个被告返回一个被告
            Wsman wm = defaultAdd(wsId, ygby);
            if (wm != null) {
                wsmen.add(wm);
                return wsmen;
            }
        } catch (Exception e) {
            e.printStackTrace( );
        }
        return null;
    }

    public List< Wsman > listByWsIdYgbgs(Integer wsId, int ygby) {
        try {
            List< Wsman > wsmanList = Wsman.dao.find("select * from wsman where wsId = ? and ygbg = ? ", wsId, ygby);
            if (wsmanList != null && wsmanList.size( ) > 0) {
                return wsmanList;
            }
        } catch (Exception e) {
            e.printStackTrace( );
        }
        return null;
    }

    /**
     * 默认添加数据信息
     *
     * @param wsId
     * @param ygby
     * @return
     */
    public Wsman defaultAdd(int wsId, int ygby) {
        try {
            Wsman m = new Wsman( );
            m.setWsId(wsId);
            m.setYgbg(ygby);
            m.setMtype("个人");
            m.setCreateAt(new Date( ));
            m.save( );
            return m;
        } catch (Exception e) {
            e.printStackTrace( );
        }
        return null;
    }

    /**
     * 查询 文书信息 所有 文书原告被告人
     * @param wsid
     * @return
     */
    public List< Wsman > listByWsid(Integer wsid) {
        try {
            return Wsman.dao.find("select * from wsman where wsId = ? order by ygbg asc,createAt desc", wsid);
        } catch (Exception e) {
            e.printStackTrace( );
        }
        return null;
    }

    /**
     * id查询
     * @param id
     * @return
     */
    public Wsman getOneById(Integer id) {
        return Wsman.dao.findById(id);
    }

    /**
     * 删除该文书与文书下的所有 原告 与被告信息
     * @param wsid
     */
    public void deletByWsid(Integer wsid) {
        Db.delete("delete from wsman where wsId = ?", wsid);
        Wsinfo.dao.deleteById(wsid);
    }

    public List< Wsman> listBymType(Integer wsid,String mtype) {
        return Wsman.dao.find("select * from wsman where wsId = ? and mtype = ?",wsid,mtype);
    }
}
