package com.jfinal.club.ws;

import com.jfinal.club.common.model.Docws;
import com.jfinal.club.common.model.Nation;
import com.jfinal.kit.Ret;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;

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

/**
 * @ClassName DocWsService
 * @Description TODO
 * @Author caolong
 * @Date 2020/3/24 15:11
 */
public class DocWsService {


    private Docws docws = new Docws( ).dao( );

    private Nation nation = new Nation( ).dao( );


    public Ret saveDocws(Docws docws) {
        if (docws.getId( ) == null) {
            if (docws.save( )) {
                Ret ret = Ret.create( );
                ret.setOk( );
                ret.put("data", docws.getId( ));
                return ret;
            } else {
                return Ret.fail("msg", "添加失败,请重新填写");
            }
        } else {
            if (docws.getLevels( ).equals(4)) {
                docws.setAddtime(new Date( ));
                docws.setStatus(0);
            }
            if (docws.update( )) {
                Ret ret = Ret.create( );
                ret.setOk( );
                ret.put("data", docws.getId( ));
                return ret;
            } else {
                return Ret.fail("msg", "修改失败,请重新填写");
            }
        }
    }

    public List< Nation > saveNation( ) {
        return nation.findAll( );
    }

    public Page< Docws > getDocwsByUsersId(int usersId, int pageNum, int status) {
        return docws.paginate(pageNum, 10, "select * ", "from docws where usersid = ? and status = ? and finish = 1 order by addtime desc", usersId, status);
    }


    public Docws getDocwsById(int docwsId) {
        return docws.findById(docwsId);
    }


    public Ret delDocwsById(int docwsId) {
        boolean b = docws.deleteById(docwsId);
        if (b) {
            return Ret.ok("msg", "删除成功");
        } else {
            return Ret.fail("msg", "删除失败");
        }
    }

    /**
     * @return java.lang.Long
     * @Author Cc
     * @Description //TODO 查看文书数量
     * @Date 2020/3/23
     * @Param [usersId]
     **/
    public Long countDocwsById(int usersId) {
        return Db.queryLong("select count(*) from docws where usersid = ? and status = 1", usersId);
    }


    /**
     * 查看完整文书
     *
     * @param usersId
     * @param finish  0不完整，1 完整
     * @return
     */
    public Docws getDocwsByFinish(int usersId, int finish) {
        try {
            return docws.findFirst("select * from docws where usersid = ? and status = 0 and finish = ?", usersId, finish);
        } catch (Exception e) {
            e.printStackTrace( );
        }
        return null;
    }

    /**
     *
     * @param userId 用户id
     * @param status 0未审核 1通过 2驳回
     * @param finish 0未完成 1完成
     * @return
     */
    public List< Docws > listDocwsByUsersId(int userId, int status, int finish) {
        try {
            return docws.find("select * from docws where usersid = ? and status = ? and finish = ?", userId, status, finish);
        } catch (Exception e) {
            e.printStackTrace( );
        }
        return null;
    }
}
