package com.ekexiu.portal.team;

import org.jfw.apt.annotation.DefaultValue;
import org.jfw.apt.annotation.Nullable;
import org.jfw.apt.orm.annotation.dao.Column;
import org.jfw.apt.orm.annotation.dao.DAO;
import org.jfw.apt.orm.annotation.dao.method.Exclude;
import org.jfw.apt.orm.annotation.dao.method.From;
import org.jfw.apt.orm.annotation.dao.method.LimitColumn;
import org.jfw.apt.orm.annotation.dao.method.OrderBy;
import org.jfw.apt.orm.annotation.dao.method.SetSentence;
import org.jfw.apt.orm.annotation.dao.method.Where;
import org.jfw.apt.orm.annotation.dao.method.operator.DeleteWith;
import org.jfw.apt.orm.annotation.dao.method.operator.Insert;
import org.jfw.apt.orm.annotation.dao.method.operator.LimitSelect;
import org.jfw.apt.orm.annotation.dao.method.operator.PageQuery;
import org.jfw.apt.orm.annotation.dao.method.operator.PageSelect;
import org.jfw.apt.orm.annotation.dao.method.operator.QueryVal;
import org.jfw.apt.orm.annotation.dao.method.operator.SelectList;
import org.jfw.apt.orm.annotation.dao.method.operator.SelectOne;
import org.jfw.apt.orm.annotation.dao.method.operator.Update;
import org.jfw.apt.orm.annotation.dao.method.operator.UpdateWith;
import org.jfw.apt.orm.annotation.dao.param.GroupSqlColumn;
import org.jfw.apt.orm.annotation.dao.param.In;
import org.jfw.apt.orm.annotation.dao.param.Like;
import org.jfw.apt.orm.annotation.dao.param.Set;
import org.jfw.apt.orm.annotation.dao.param.SqlColumn;
import org.jfw.apt.orm.core.defaultImpl.LongHandler;
import org.jfw.apt.orm.core.defaultImpl.StringHandler;
import org.jfw.util.PageQueryResult;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

/**
 * Created by TT on 2019/7/8.
 */
@DAO
public interface TeamDao {

    @Insert
    int insert(Connection con, Team team) throws SQLException;

    @Update
    @Exclude({"secretary","chief","status","certify"})
    int update(Connection con, Team team) throws SQLException;

    @UpdateWith
    @From(Team.class)
    @Where("STATUS <>'0'")
    @SetSentence("STATUS='0'")
    int delete(Connection con, String id) throws SQLException;

    @UpdateWith
    @From(Team.class)
    @Where("STATUS <> '0'")
    int updateStatus(Connection con, @Set String status, String id) throws SQLException;

    @UpdateWith
    @From(Team.class)
    int updateChief(Connection con,@Nullable @Set String chief, String id) throws SQLException;

    @UpdateWith
    @From(Team.class)
    int updateSecretary(Connection con, @Set String secretary, String id) throws SQLException;


    @SelectOne
    @Nullable
    Team query(Connection con, String id) throws SQLException;

    @SelectList
    List<Team> query(Connection con, @In String[] id) throws SQLException;

    @PageSelect
    @OrderBy("ORDER BY CREATE_TIME DESC")
    PageQueryResult<Team> pageQuery(Connection con,@Nullable @In String[] status, @Nullable @GroupSqlColumn(handlerClass = StringHandler.class, value = {"name like ?", "org_name like ?", "subject like ?", "industry like ?"},isAnd = false) String key, @Nullable @Like String subject, @Nullable @Like String industry, @Nullable String city, int pageSize, int pageNo) throws SQLException;

    @PageQuery
    @OrderBy("ORDER BY SECRETARY DESC NULLS LAST, CREATE_TIME DESC")
    PageQueryResult<Team> pageQuery(Connection con,@Nullable @In String[] status, @SqlColumn(handlerClass = StringHandler.class, value = "id in (select id from team_pro where professor = ?)") String professor, int pageSize, int pageNo) throws SQLException;

    @Insert
    int insert(Connection con, TeamPro teamPro) throws SQLException;

    @PageSelect
    @OrderBy("ORDER BY SECRETARY DESC NULLS LAST,CHIEF DESC NULLS LAST,ID DESC")
    PageQueryResult<TeamPro> queryPro(Connection con, String id, int pageSize, int pageNo) throws SQLException;

    @DeleteWith
    @From(TeamPro.class)
    int deletePro(Connection con, String id,String professor) throws SQLException;

    @Insert
    int insert(Connection con, TeamPatent teamPatent) throws SQLException;

    @UpdateWith
    @From(TeamPro.class)
    @Where("secretary ='1'")
    @SetSentence("secretary='0'")
    int update(Connection con, String id) throws SQLException;

    @UpdateWith
    @From(TeamPro.class)
    @SetSentence("secretary='1'")
    int update(Connection con, String id,String professor) throws SQLException;

    @UpdateWith
    @From(TeamPro.class)
    @Where("chief ='1'")
    @SetSentence("chief='0'")
    int updateCh(Connection con, String id) throws SQLException;

    @UpdateWith
    @From(TeamPro.class)
    @SetSentence("chief='1'")
    int updateCh(Connection con, String id,String professor) throws SQLException;

    @PageSelect
    @OrderBy("order by patent")
    PageQueryResult<TeamPatent> queryPatent(Connection con, String id, int pageSize, int pageNo) throws SQLException;

    @DeleteWith
    @From(TeamPatent.class)
    int deletePatent(Connection con, String id,String patent) throws SQLException;

    @Insert
    int insert(Connection con, TeamResResult teamResResult) throws SQLException;

    @PageSelect
    @OrderBy("order by id")
    PageQueryResult<TeamResResult> queryResResult(Connection con, String id, int pageSize, int pageNo) throws SQLException;

    @DeleteWith
    @From(TeamResResult.class)
    int deleteResResult(Connection con, String id, String researchResult) throws SQLException;

    @Insert
    int insert(Connection con, TeamPaper teamPaper) throws SQLException;

    @PageSelect
    @OrderBy("order by id")
    PageQueryResult<TeamPaper> queryPaper(Connection con, String id, int pageSize, int pageNo) throws SQLException;

    @DeleteWith
    @From(TeamPaper.class)
    int deletePaper(Connection con, String id, String paper) throws SQLException;

    @DefaultValue("0")
    @QueryVal
    @Column(value = "COUNT(1)", handlerClass = LongHandler.class)
    @From(TeamPro.class)
    long countPro(Connection con, String id) throws SQLException;

    @LimitSelect
    @Where("STATUS='3'")
    @OrderBy(cols = { @LimitColumn(value = "createTime", asc = false),
            @LimitColumn(value = "id", asc = false) }, value = "")
    List<Team> indexSearch(Connection con, @Nullable @GroupSqlColumn(handlerClass = StringHandler.class, value = {"name like ?", "org_name like ?", "subject like ?", "industry like ?"},isAnd = false) String key,@Nullable @Like String subject, @Nullable @Like String industry, @Nullable String city, String createTime, String id, int rows) throws SQLException;
}