<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.xhkjedu.sapi.mapper.cloud.CloudSyncMapper">
  <!--列表-->
  <resultMap id="syncResult" type="java.util.Map">
    <id column="csid" property="csid"></id>
    <collection property="details" javaType="java.util.List" select="listDetail" column="csid">
    </collection>
  </resultMap>
  <select id="listDetail" resultType="java.util.Map">
    select csdid,csid,plannum,beginid,endid,syncnum,realnum,handleid,starttime,stoptime
    ,begintime,endtime,d.createid,d.createtime,sb.lsbname
    ,(select s.subjectname from t_subject s where s.subjectid=sb.subjectid)subjectname
    ,(select v.versionname from t_version v where v.versionid=sb.versionid)versionname
    ,(case synctable when 't_subject' then '科目' when 't_version' then '版本' when 't_subject_book' then '教材'
    when 't_director' then '章节' when 't_point' then '知识点' when 't_subject_questiontype' then '题型'
    when 't_resource' then '资源' when 't_question' then '试题' else synctable end)synctable
    from t_cloud_sync_detail d left join t_subject_book sb on d.lsbid=sb.lsbid
    where csid=#{csid}
  </select>
  <select id="findAll" resultMap="syncResult">
    select csid,begintime,endtime,starttime,stoptime,synctask,syncstate,createid,createtime
    from t_cloud_sync order by csid desc
  </select>
  <!--添加-->
  <insert id="save">
    insert into t_cloud_sync(csid,begintime,endtime,starttime,stoptime,synctask,syncstate,createid,createtime)
    values (#{sync.csid},#{sync.begintime},#{sync.endtime},#{sync.starttime},#{sync.stoptime}
    ,#{sync.synctask},#{sync.syncstate},#{sync.createid},#{sync.createtime})
  </insert>
  <!--获取最后一条同步任务-->
  <select id="getLastSync" resultType="com.xhkjedu.sapi.model.cloud.TCloudSync">
    select csid, begintime, endtime, starttime, stoptime, synctask, syncstate, createid, createtime
    from t_cloud_sync order by csid desc limit 1
  </select>
  <!--更新同步状态-->
  <update id="updateState">
    update t_cloud_sync set syncstate=#{syncstate},stoptime=UNIX_TIMESTAMP(now()) where csid=#{csid}
  </update>
  <select id="getCloudInfo" resultType="com.xhkjedu.sapi.model.cloud.TCloudSync">
      select (select userid from t_user where usertype=999 limit 1)createid
      ,(select cloudcode from t_cloud limit 1)cloudcode
  </select>
    <!--同步科目-->
  <insert id="syncSubject" parameterType="com.xhkjedu.sapi.model.subjectbook.TSubject">
    <foreach collection ="list" item="m" index= "index" separator =";">
      INSERT INTO t_subject (subjectid,subjectname,level,subjectorder,createtime,timeunix,belong)
      VALUES (#{m.subjectid},#{m.subjectname},#{m.level},#{m.subjectorder},#{m.handletime},UNIX_TIMESTAMP(now()),1)
      ON DUPLICATE KEY UPDATE subjectname=#{m.subjectname},subjectorder=#{m.subjectorder}
    </foreach>
  </insert>
  <!--同步版本-->
  <insert id="syncVersion" parameterType="com.xhkjedu.sapi.model.subjectbook.TVersion">
    <foreach collection ="list" item="m" index= "index" separator =";">
      INSERT INTO t_version (versionid,versionname,versionorder,createtime,timeunix,belong)
      VALUES (#{m.versionid},#{m.versionname},#{m.versionorder},#{m.handletime},UNIX_TIMESTAMP(now()),1)
      ON DUPLICATE KEY UPDATE versionname=#{m.versionname},versionorder=#{m.versionorder}
    </foreach>
  </insert>
  <!--同步教材-->
  <insert id="syncSubjectBook" parameterType="com.xhkjedu.sapi.model.subjectbook.TSubjectBook">
    <foreach collection ="list" item="m" index= "index" separator =";">
      INSERT INTO t_subject_book (lsbid,level,subjectid,versionid,lsbname,lsborder,coverpath,createtime,timeunix,belong)
      VALUES (#{m.lsbid},#{m.level},#{m.subjectid},#{m.versionid},#{m.lsbname},#{m.lsborder},#{m.coverpath}
      ,#{m.handletime},UNIX_TIMESTAMP(now()),1)
      ON DUPLICATE KEY UPDATE lsbname=#{m.lsbname},lsborder=#{m.lsborder},coverpath=#{m.coverpath}
    </foreach>
  </insert>
  <!--同步题型-->
  <insert id="syncSubjectQuestionType" parameterType="com.xhkjedu.sapi.model.subjectbook.TSubjectQuestiontype">
    <foreach collection ="list" item="m" index= "index" separator =";">
      INSERT INTO t_subject_questiontype (qtypeid,qtypename,qtypeorder,subjectid,ctype,createtime,timeunix,belong)
      VALUES (#{m.qtypeid},#{m.qtypename},#{m.qtypeorder},#{m.subjectid},#{m.ctype}
      ,#{m.handletime},UNIX_TIMESTAMP(now()),1)
      ON DUPLICATE KEY UPDATE qtypename=#{m.qtypename},qtypeorder=#{m.qtypeorder},ctype=#{m.ctype}
    </foreach>
  </insert>
  <!--同步章节-->
  <insert id="syncDirector" parameterType="com.xhkjedu.sapi.model.subjectbook.TDirector">
    <foreach collection ="list" item="m" index= "index" separator =";">
      INSERT INTO t_director (directorid,directorname,directorpid,directororder,leaf
      ,lsbid,directorlevel,directorall,createtime,timeunix,belong)
      VALUES (#{m.directorid},#{m.directorname},#{m.directorpid},#{m.directororder},#{m.leaf}
      ,#{m.lsbid},#{m.directorlevel},#{m.directorall},#{m.handletime},UNIX_TIMESTAMP(now()),1)
      ON DUPLICATE KEY UPDATE directorname=#{m.directorname},directororder=#{m.directororder}
      ,leaf=#{m.leaf},directorall=#{m.directorall}
    </foreach>
  </insert>
  <!--同步知识点-->
  <insert id="syncPoint" parameterType="com.xhkjedu.sapi.model.subjectbook.TPoint">
    <foreach collection ="list" item="m" index= "index" separator =";">
      INSERT INTO t_point (pointid,pointname,pointorder,pointpid,subjectid
      ,pointlevel,createtime,timeunix,belong)
      VALUES (#{m.pointid},#{m.pointname},#{m.pointorder},#{m.pointpid},#{m.subjectid}
      ,#{m.pointlevel},#{m.handletime},UNIX_TIMESTAMP(now()),1)
      ON DUPLICATE KEY UPDATE pointname=#{m.pointname},pointorder=#{m.pointorder}
    </foreach>
  </insert>
  <!--同步试题-->
  <insert id="syncQuestion" parameterType="com.xhkjedu.sapi.model.subjectbook.TQuestion">
      INSERT INTO t_question (questionid,qstem,qstemtxt,qoption,qanswer,qanalyze,score
      ,complexity,ctype,qtypeid,qtypename,subjectid,qlevel,qstate,createtime,timeunix
      ,source,year,region,schoolname,belong)
      VALUES (#{m.questionid},#{m.qstem},#{m.qstemtxt},#{m.qoption},#{m.qanswer},#{m.qanalyze},#{m.score},#{m.complexity}
      ,#{m.ctype},#{m.qtypeid},#{m.qtypename},#{m.subjectid},#{m.qlevel},#{m.qstate}
      ,#{m.handletime},UNIX_TIMESTAMP(now()),#{m.source},#{m.year},#{m.region},#{m.schoolname},1)
      ON DUPLICATE KEY UPDATE qstem=#{m.qstem},qstemtxt=#{m.qstemtxt},qoption=#{m.qoption}
      ,qanswer=#{m.qanswer},qanalyze=#{m.qanalyze},score=#{m.score},complexity=#{m.complexity},qstate=#{m.qstate}
      ,source=#{m.source},year=#{m.year},region=#{m.region},schoolname=#{m.schoolname}
  </insert>
  <!--同步试题章节-->
  <insert id="syncQuestionDirector" parameterType="com.xhkjedu.sapi.model.subjectbook.TQuestionDirector">
      <foreach collection ="list" item="m" index= "index" separator =";">
      INSERT INTO t_question_director (qdid,questionid,lsbid,directorid,createtime,timeunix)
      VALUES (#{m.qdid},#{m.questionid},#{m.lsbid},#{m.directorid},#{m.handletime},UNIX_TIMESTAMP(now()))
      ON DUPLICATE KEY UPDATE questionid=#{m.questionid},lsbid=#{m.lsbid},directorid=#{m.directorid}
      </foreach>
  </insert>
  <!--同步试题知识点-->
  <insert id="syncQuestionPoint" parameterType="com.xhkjedu.sapi.model.subjectbook.TQuestionPoint">
      <foreach collection ="list" item="m" index= "index" separator =";">
      INSERT INTO t_question_point (qpid,questionid,pointid,createtime,timeunix)
      VALUES (#{m.qpid},#{m.questionid},#{m.pointid},#{m.handletime},UNIX_TIMESTAMP(now()))
      ON DUPLICATE KEY UPDATE questionid=#{m.questionid},pointid=#{m.pointid}
      </foreach>
  </insert>
  <!--同步资源-->
  <insert id="syncResource" parameterType="com.xhkjedu.sapi.model.subjectbook.TResource">
      INSERT INTO t_resource (resourceid,resourcename,subjectid,resourceclass,resourcetype,level
      ,resourceurl,resourcecover,converted,pdfurl,resourcestate,suffix,mp4code,resourcesize,duration,comm
      ,createtime,timeunix,resourcegrade,source,year,region,schoolname,belong)
      VALUES (#{m.resourceid},#{m.resourcename},#{m.subjectid},#{m.resourceclass},#{m.resourcetype},#{m.level}
      ,#{m.resourceurl},#{m.resourcecover},#{m.converted},#{m.pdfurl},#{m.resourcestate},#{m.suffix}
      ,#{m.mp4code},#{m.resourcesize},#{m.duration},#{m.comm},#{m.handletime},UNIX_TIMESTAMP(now())
      ,#{m.resourcegrade},#{m.source},#{m.year},#{m.region},#{m.schoolname},1)
      ON DUPLICATE KEY UPDATE resourcename=#{m.resourcename},resourcestate=#{m.resourcestate},comm=#{m.comm}
  </insert>
  <!--同步资源章节-->
  <insert id="syncResourceDirector" parameterType="com.xhkjedu.sapi.model.subjectbook.TResourceDirector">
      <foreach collection ="list" item="m" index= "index" separator =";">
      INSERT INTO t_resource_director (rdid,resourceid,lsbid,directorid,createtime,timeunix)
      VALUES (#{m.rdid},#{m.resourceid},#{m.lsbid},#{m.directorid},#{m.handletime},UNIX_TIMESTAMP(now()))
      ON DUPLICATE KEY UPDATE resourceid=#{m.resourceid},lsbid=#{m.lsbid},directorid=#{m.directorid}
      </foreach>
  </insert>
    <!--外键是否存在-->
    <select id="getFkExisted" resultType="java.lang.Integer">
        SELECT 1 FROM information_schema.KEY_COLUMN_USAGE WHERE CONSTRAINT_NAME=#{fkname} and CONSTRAINT_SCHEMA=#{dbname}
    </select>
    <!--删除章节外键-->
    <update id="delDirectorFk">
        ALTER TABLE t_director DROP FOREIGN KEY fk_directorpid;
    </update>
    <!--添加章节外键-->
    <update id="addDirectorFk">
        ALTER TABLE t_director ADD CONSTRAINT fk_directorpid
        FOREIGN KEY (directorpid) REFERENCES t_director(directorid) ON DELETE CASCADE ON UPDATE CASCADE;
    </update>
    <!--删除知识点外键-->
    <update id="delPointFk">
        ALTER TABLE t_point DROP FOREIGN KEY fk_pointpid;
    </update>
    <!--添加知识点外键-->
    <update id="addPointFk">
        ALTER TABLE t_point ADD CONSTRAINT fk_pointpid
        FOREIGN KEY (pointpid) REFERENCES t_point(pointid) ON DELETE CASCADE ON UPDATE CASCADE;
    </update>
</mapper>
