package com.ho.business.mapper;


import com.ho.business.entity.TabSort;
import com.ho.business.entity.TabTag;
import com.ho.business.vo.req.TabReqVo;
import com.ho.business.vo.req.TabSortReqVo;
import org.apache.ibatis.annotations.*;

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

@Mapper
public interface TabSortWebMapper {

    @Insert("insert into tab_sort(tab_name,tab_order,tab_status,station_id,commissioning,version_no,status) values(#{tabName},#{tabOrder},#{tabStatus},#{stationId},#{commissioning},#{versionNo},#{status})")
    @Options(useGeneratedKeys=true, keyProperty="tabId", keyColumn="tab_id")
    int insert(TabSort tabSort);

    @Update("update tab_sort set tab_name=#{tabName},tab_order=#{tabOrder}  where tab_id=#{tabId}")
    int updateByTabId(TabSortReqVo tabSortReqVo);

    @Delete("delete from tab_sort where tab_id=#{tabId}")
    int deleteTabSort(@Param("tabId") Integer tabId);

    @Select("select * from tab_sort where station_id=#{stationId} and status=1 order by tab_order")
    List<TabSort> queryTabSortList(@Param("stationId") Integer stationId);

    @Update("update tab_sort set status=0  where station_id=#{stationId} and version_no!=#{versionNo}")
    int toggleVersion(TabReqVo tabReqVo);

    @Update("update tab_sort set status=1  where station_id=#{stationId} and version_no=#{versionNo}")
    int updateVersion(TabReqVo tabReqVo);

    @Update("update tab_sort set status=0  where station_id=#{stationId}")
    int updateExclVersion(@Param("stationId") Integer stationId);

    @Select("select DISTINCT version_no,station_id,status from tab_sort where station_id=#{stationId}")
    List<Map<String,Object>> queryVersion(@Param("stationId") Integer stationId);

    @Select("select * from tab_sort where station_id=#{stationId} and version_no=#{versionNo}")
    List<TabSort> showTabSortList(@Param("stationId") Integer stationId,@Param("versionNo") Date versionNo);

    @Select("select version_no from tab_sort where station_id=#{stationId} group by version_no order by version_no")
    List<TabSort> getList(@Param("stationId") Integer stationId);

    @Delete("delete from tab_sort where station_id=#{stationId} and version_no=#{versionNo}")
    int deleteTabSortLater(@Param("stationId") Integer stationId,@Param("versionNo") Date versionNo);

    @Select({"<script>",
            "select count(1) from tab_sort where tab_name = #{tabName} and status = 1" +
                    "<when test='tabId != null'>"+
                    " and tab_id != #{tabId}" +
                    "</when>" +
                    "<when test='stationId != null'>"+
                    " and station_id = #{stationId}" +
                    "</when>" +
            "</script>"})
    int checkTabName(@Param("stationId")Integer stationId,@Param("tabName")String tabName,@Param("tabId")Integer tabId);

    @Select("select * from tab_sort where tab_id = #{tabId}")
    TabSort getTabSortById(Integer tabId);

    @Select("select tab_id from tab_tag where tag_id = #{tagId}")
    Integer getTabIdByTagId(Integer tagId);

    @Update("update tab_tag set changeover = 0 where tab_id = #{tabId}")
    int updateChangeover(Integer tabId);

    @Update("update tab_tag set changeover = 1 where tag_id = #{tagId}")
    void chooseState(Integer tagId);

    @Select(" select * from tab_tag where changeover = 1 and tab_id = #{tabId} limit 1")
    TabTag queryTabTag(Integer tabId);

    @Select(" select count(1) from tab_tag where tab_id = #{tabId} and changeover = 1 ")
    int queryTabTagIsChangeover(Integer tabId);

}
