<?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.goafanti.common.dao.PublicReleaseMapper">
  <resultMap id="BaseResultMap" type="com.goafanti.common.model.PublicRelease">
    <id column="id" jdbcType="INTEGER" property="id" />
    <result column="aid" jdbcType="VARCHAR" property="aid" />
    <result column="release_start" jdbcType="TIMESTAMP" property="releaseStart" />
    <result column="release_end" jdbcType="TIMESTAMP" property="releaseEnd" />
    <result column="remarks" jdbcType="VARCHAR" property="remarks" />
    <result column="annex_url" jdbcType="VARCHAR" property="annexUrl" />
    <result column="status" jdbcType="INTEGER" property="status" />
    <result column="create_time" jdbcType="TIMESTAMP" property="createTime" />
    <result column="pid" jdbcType="VARCHAR" property="pid" />
    <result column="pname" jdbcType="VARCHAR" property="pname" />
    <result column="duration" jdbcType="DOUBLE" property="duration" />
    <result column="valid_date" jdbcType="VARCHAR" property="validDate" />
    <result column="supplement" jdbcType="VARCHAR" property="supplement" />
    <result column="audit_info" jdbcType="VARCHAR" property="auditInfo" />
    <result column="plan" jdbcType="VARCHAR" property="plan" />
    <result column="expected_effect" jdbcType="VARCHAR" property="expectedEffect" />
    <result column="next_plan" jdbcType="VARCHAR" property="nextPlan" />
    <result column="type" jdbcType="INTEGER" property="type" />
    <result column="order_no" jdbcType="VARCHAR" property="orderNo" />
    <result column="update_time" jdbcType="TIMESTAMP" property="updateTime" />
    <result column="update_status" jdbcType="INTEGER" property="updateStatus" />
    <result column="assist" jdbcType="INTEGER" property="assist" />
    <result column="assist_aid" jdbcType="VARCHAR" property="assistAid" />
    <result column="assist_aid_name" jdbcType="VARCHAR" property="assistAidName" />
    <result column="main_id" jdbcType="INTEGER" property="mainId" />
    <result column="main_name" jdbcType="VARCHAR" property="mainName" />
    <result column="reject_name" jdbcType="VARCHAR" property="rejectName" />
    <result column="assistant_aid" jdbcType="VARCHAR" property="assistantAid" />
    <result column="assistant_name" jdbcType="VARCHAR" property="assistantName" />
    <result column="public_again" jdbcType="INTEGER" property="publicAgain" />
    <result column="audit_time" jdbcType="TIMESTAMP" property="auditTime" />
    <result column="new_user" jdbcType="VARCHAR" property="newUser" />
    <result column="sign_sum" jdbcType="VARCHAR" property="signSum" />
    <result column="user_names" jdbcType="VARCHAR" property="userNames" />
    <result column="object_type" jdbcType="INTEGER" property="objectType" />
    <result column="public_type" jdbcType="INTEGER" property="publicType" />
    <result column="district_name" jdbcType="VARCHAR" property="districtName" />
    <result column="clock_in" jdbcType="INTEGER" property="clockIn" />
  </resultMap>
  <sql id="Base_Column_List">
    id, aid, release_start, release_end, remarks, annex_url, `status`, create_time, pid,
    pname, duration, valid_date, supplement, audit_info, `plan`, expected_effect, next_plan,
    `type`, order_no, update_time, update_status, assist, assist_aid, assist_aid_name,
    main_id, main_name, reject_name, assistant_aid, assistant_name, public_again, audit_time,
    new_user, sign_sum, user_names, object_type, public_type, district_name, clock_in
  </sql>
  <select id="selectByPrimaryKey" parameterType="java.lang.Integer" resultMap="BaseResultMap">
    select
    <include refid="Base_Column_List" />
    from public_release
    where id = #{id,jdbcType=INTEGER}
  </select>
  <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer">
    delete from public_release
    where id = #{id,jdbcType=INTEGER}
  </delete>
  <insert id="insert" keyColumn="id" keyProperty="id" parameterType="com.goafanti.common.model.PublicRelease" useGeneratedKeys="true">
    insert into public_release (aid, release_start, release_end,
      remarks, annex_url, `status`,
      create_time, pid, pname,
      duration, valid_date, supplement,
      audit_info, `plan`, expected_effect,
      next_plan, `type`, order_no,
      update_time, update_status, assist,
      assist_aid, assist_aid_name, main_id,
      main_name, reject_name, assistant_aid,
      assistant_name, public_again, audit_time,
      new_user, sign_sum, user_names,
      object_type, public_type, district_name,
      clock_in)
    values (#{aid,jdbcType=VARCHAR}, #{releaseStart,jdbcType=TIMESTAMP}, #{releaseEnd,jdbcType=TIMESTAMP},
      #{remarks,jdbcType=VARCHAR}, #{annexUrl,jdbcType=VARCHAR}, #{status,jdbcType=INTEGER},
      #{createTime,jdbcType=TIMESTAMP}, #{pid,jdbcType=VARCHAR}, #{pname,jdbcType=VARCHAR},
      #{duration,jdbcType=DOUBLE}, #{validDate,jdbcType=VARCHAR}, #{supplement,jdbcType=VARCHAR},
      #{auditInfo,jdbcType=VARCHAR}, #{plan,jdbcType=VARCHAR}, #{expectedEffect,jdbcType=VARCHAR},
      #{nextPlan,jdbcType=VARCHAR}, #{type,jdbcType=INTEGER}, #{orderNo,jdbcType=VARCHAR},
      #{updateTime,jdbcType=TIMESTAMP}, #{updateStatus,jdbcType=INTEGER}, #{assist,jdbcType=INTEGER},
      #{assistAid,jdbcType=VARCHAR}, #{assistAidName,jdbcType=VARCHAR}, #{mainId,jdbcType=INTEGER},
      #{mainName,jdbcType=VARCHAR}, #{rejectName,jdbcType=VARCHAR}, #{assistantAid,jdbcType=VARCHAR},
      #{assistantName,jdbcType=VARCHAR}, #{publicAgain,jdbcType=INTEGER}, #{auditTime,jdbcType=TIMESTAMP},
      #{newUser,jdbcType=VARCHAR}, #{signSum,jdbcType=VARCHAR}, #{userNames,jdbcType=VARCHAR},
      #{objectType,jdbcType=INTEGER}, #{publicType,jdbcType=INTEGER}, #{districtName,jdbcType=VARCHAR},
      #{clockIn,jdbcType=INTEGER})
  </insert>
  <insert id="insertSelective" keyColumn="id" keyProperty="id" parameterType="com.goafanti.common.model.PublicRelease" useGeneratedKeys="true">
    insert into public_release
    <trim prefix="(" suffix=")" suffixOverrides=",">
      <if test="aid != null">
        aid,
      </if>
      <if test="releaseStart != null">
        release_start,
      </if>
      <if test="releaseEnd != null">
        release_end,
      </if>
      <if test="remarks != null">
        remarks,
      </if>
      <if test="annexUrl != null">
        annex_url,
      </if>
      <if test="status != null">
        `status`,
      </if>
      <if test="createTime != null">
        create_time,
      </if>
      <if test="pid != null">
        pid,
      </if>
      <if test="pname != null">
        pname,
      </if>
      <if test="duration != null">
        duration,
      </if>
      <if test="validDate != null">
        valid_date,
      </if>
      <if test="supplement != null">
        supplement,
      </if>
      <if test="auditInfo != null">
        audit_info,
      </if>
      <if test="plan != null">
        `plan`,
      </if>
      <if test="expectedEffect != null">
        expected_effect,
      </if>
      <if test="nextPlan != null">
        next_plan,
      </if>
      <if test="type != null">
        `type`,
      </if>
      <if test="orderNo != null">
        order_no,
      </if>
      <if test="updateTime != null">
        update_time,
      </if>
      <if test="updateStatus != null">
        update_status,
      </if>
      <if test="assist != null">
        assist,
      </if>
      <if test="assistAid != null">
        assist_aid,
      </if>
      <if test="assistAidName != null">
        assist_aid_name,
      </if>
      <if test="mainId != null">
        main_id,
      </if>
      <if test="mainName != null">
        main_name,
      </if>
      <if test="rejectName != null">
        reject_name,
      </if>
      <if test="assistantAid != null">
        assistant_aid,
      </if>
      <if test="assistantName != null">
        assistant_name,
      </if>
      <if test="publicAgain != null">
        public_again,
      </if>
      <if test="auditTime != null">
        audit_time,
      </if>
      <if test="newUser != null">
        new_user,
      </if>
      <if test="signSum != null">
        sign_sum,
      </if>
      <if test="userNames != null">
        user_names,
      </if>
      <if test="objectType != null">
        object_type,
      </if>
      <if test="publicType != null">
        public_type,
      </if>
      <if test="districtName != null">
        district_name,
      </if>
      <if test="clockIn != null">
        clock_in,
      </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides=",">
      <if test="aid != null">
        #{aid,jdbcType=VARCHAR},
      </if>
      <if test="releaseStart != null">
        #{releaseStart,jdbcType=TIMESTAMP},
      </if>
      <if test="releaseEnd != null">
        #{releaseEnd,jdbcType=TIMESTAMP},
      </if>
      <if test="remarks != null">
        #{remarks,jdbcType=VARCHAR},
      </if>
      <if test="annexUrl != null">
        #{annexUrl,jdbcType=VARCHAR},
      </if>
      <if test="status != null">
        #{status,jdbcType=INTEGER},
      </if>
      <if test="createTime != null">
        #{createTime,jdbcType=TIMESTAMP},
      </if>
      <if test="pid != null">
        #{pid,jdbcType=VARCHAR},
      </if>
      <if test="pname != null">
        #{pname,jdbcType=VARCHAR},
      </if>
      <if test="duration != null">
        #{duration,jdbcType=DOUBLE},
      </if>
      <if test="validDate != null">
        #{validDate,jdbcType=VARCHAR},
      </if>
      <if test="supplement != null">
        #{supplement,jdbcType=VARCHAR},
      </if>
      <if test="auditInfo != null">
        #{auditInfo,jdbcType=VARCHAR},
      </if>
      <if test="plan != null">
        #{plan,jdbcType=VARCHAR},
      </if>
      <if test="expectedEffect != null">
        #{expectedEffect,jdbcType=VARCHAR},
      </if>
      <if test="nextPlan != null">
        #{nextPlan,jdbcType=VARCHAR},
      </if>
      <if test="type != null">
        #{type,jdbcType=INTEGER},
      </if>
      <if test="orderNo != null">
        #{orderNo,jdbcType=VARCHAR},
      </if>
      <if test="updateTime != null">
        #{updateTime,jdbcType=TIMESTAMP},
      </if>
      <if test="updateStatus != null">
        #{updateStatus,jdbcType=INTEGER},
      </if>
      <if test="assist != null">
        #{assist,jdbcType=INTEGER},
      </if>
      <if test="assistAid != null">
        #{assistAid,jdbcType=VARCHAR},
      </if>
      <if test="assistAidName != null">
        #{assistAidName,jdbcType=VARCHAR},
      </if>
      <if test="mainId != null">
        #{mainId,jdbcType=INTEGER},
      </if>
      <if test="mainName != null">
        #{mainName,jdbcType=VARCHAR},
      </if>
      <if test="rejectName != null">
        #{rejectName,jdbcType=VARCHAR},
      </if>
      <if test="assistantAid != null">
        #{assistantAid,jdbcType=VARCHAR},
      </if>
      <if test="assistantName != null">
        #{assistantName,jdbcType=VARCHAR},
      </if>
      <if test="publicAgain != null">
        #{publicAgain,jdbcType=INTEGER},
      </if>
      <if test="auditTime != null">
        #{auditTime,jdbcType=TIMESTAMP},
      </if>
      <if test="newUser != null">
        #{newUser,jdbcType=VARCHAR},
      </if>
      <if test="signSum != null">
        #{signSum,jdbcType=VARCHAR},
      </if>
      <if test="userNames != null">
        #{userNames,jdbcType=VARCHAR},
      </if>
      <if test="objectType != null">
        #{objectType,jdbcType=INTEGER},
      </if>
      <if test="publicType != null">
        #{publicType,jdbcType=INTEGER},
      </if>
      <if test="districtName != null">
        #{districtName,jdbcType=VARCHAR},
      </if>
      <if test="clockIn != null">
        #{clockIn,jdbcType=INTEGER},
      </if>
    </trim>
  </insert>
  <update id="updateByPrimaryKeySelective" parameterType="com.goafanti.common.model.PublicRelease">
    update public_release
    <set>
      <if test="aid != null">
        aid = #{aid,jdbcType=VARCHAR},
      </if>
      <if test="releaseStart != null">
        release_start = #{releaseStart,jdbcType=TIMESTAMP},
      </if>
      <if test="releaseEnd != null">
        release_end = #{releaseEnd,jdbcType=TIMESTAMP},
      </if>
      <if test="remarks != null">
        remarks = #{remarks,jdbcType=VARCHAR},
      </if>
      <if test="annexUrl != null">
        annex_url = #{annexUrl,jdbcType=VARCHAR},
      </if>
      <if test="status != null">
        `status` = #{status,jdbcType=INTEGER},
      </if>
      <if test="createTime != null">
        create_time = #{createTime,jdbcType=TIMESTAMP},
      </if>
      <if test="pid != null">
        pid = #{pid,jdbcType=VARCHAR},
      </if>
      <if test="pname != null">
        pname = #{pname,jdbcType=VARCHAR},
      </if>
      <if test="duration != null">
        duration = #{duration,jdbcType=DOUBLE},
      </if>
      <if test="validDate != null">
        valid_date = #{validDate,jdbcType=VARCHAR},
      </if>
      <if test="supplement != null">
        supplement = #{supplement,jdbcType=VARCHAR},
      </if>
      <if test="auditInfo != null">
        audit_info = #{auditInfo,jdbcType=VARCHAR},
      </if>
      <if test="plan != null">
        `plan` = #{plan,jdbcType=VARCHAR},
      </if>
      <if test="expectedEffect != null">
        expected_effect = #{expectedEffect,jdbcType=VARCHAR},
      </if>
      <if test="nextPlan != null">
        next_plan = #{nextPlan,jdbcType=VARCHAR},
      </if>
      <if test="type != null">
        `type` = #{type,jdbcType=INTEGER},
      </if>
      <if test="orderNo != null">
        order_no = #{orderNo,jdbcType=VARCHAR},
      </if>
      <if test="updateTime != null">
        update_time = #{updateTime,jdbcType=TIMESTAMP},
      </if>
      <if test="updateStatus != null">
        update_status = #{updateStatus,jdbcType=INTEGER},
      </if>
      <if test="assist != null">
        assist = #{assist,jdbcType=INTEGER},
      </if>
      <if test="assistAid != null">
        assist_aid = #{assistAid,jdbcType=VARCHAR},
      </if>
      <if test="assistAidName != null">
        assist_aid_name = #{assistAidName,jdbcType=VARCHAR},
      </if>
      <if test="mainId != null">
        main_id = #{mainId,jdbcType=INTEGER},
      </if>
      <if test="mainName != null">
        main_name = #{mainName,jdbcType=VARCHAR},
      </if>
      <if test="rejectName != null">
        reject_name = #{rejectName,jdbcType=VARCHAR},
      </if>
      <if test="assistantAid != null">
        assistant_aid = #{assistantAid,jdbcType=VARCHAR},
      </if>
      <if test="assistantName != null">
        assistant_name = #{assistantName,jdbcType=VARCHAR},
      </if>
      <if test="publicAgain != null">
        public_again = #{publicAgain,jdbcType=INTEGER},
      </if>
      <if test="auditTime != null">
        audit_time = #{auditTime,jdbcType=TIMESTAMP},
      </if>
      <if test="newUser != null">
        new_user = #{newUser,jdbcType=VARCHAR},
      </if>
      <if test="signSum != null">
        sign_sum = #{signSum,jdbcType=VARCHAR},
      </if>
      <if test="userNames != null">
        user_names = #{userNames,jdbcType=VARCHAR},
      </if>
      <if test="objectType != null">
        object_type = #{objectType,jdbcType=INTEGER},
      </if>
      <if test="publicType != null">
        public_type = #{publicType,jdbcType=INTEGER},
      </if>
      <if test="districtName != null">
        district_name = #{districtName,jdbcType=VARCHAR},
      </if>
      <if test="clockIn != null">
        clock_in = #{clockIn,jdbcType=INTEGER},
      </if>
    </set>
    where id = #{id,jdbcType=INTEGER}
  </update>
  <update id="updateByPrimaryKey" parameterType="com.goafanti.common.model.PublicRelease">
    update public_release
    set aid = #{aid,jdbcType=VARCHAR},
      release_start = #{releaseStart,jdbcType=TIMESTAMP},
      release_end = #{releaseEnd,jdbcType=TIMESTAMP},
      remarks = #{remarks,jdbcType=VARCHAR},
      annex_url = #{annexUrl,jdbcType=VARCHAR},
      `status` = #{status,jdbcType=INTEGER},
      create_time = #{createTime,jdbcType=TIMESTAMP},
      pid = #{pid,jdbcType=VARCHAR},
      pname = #{pname,jdbcType=VARCHAR},
      duration = #{duration,jdbcType=DOUBLE},
      valid_date = #{validDate,jdbcType=VARCHAR},
      supplement = #{supplement,jdbcType=VARCHAR},
      audit_info = #{auditInfo,jdbcType=VARCHAR},
      `plan` = #{plan,jdbcType=VARCHAR},
      expected_effect = #{expectedEffect,jdbcType=VARCHAR},
      next_plan = #{nextPlan,jdbcType=VARCHAR},
      `type` = #{type,jdbcType=INTEGER},
      order_no = #{orderNo,jdbcType=VARCHAR},
      update_time = #{updateTime,jdbcType=TIMESTAMP},
      update_status = #{updateStatus,jdbcType=INTEGER},
      assist = #{assist,jdbcType=INTEGER},
      assist_aid = #{assistAid,jdbcType=VARCHAR},
      assist_aid_name = #{assistAidName,jdbcType=VARCHAR},
      main_id = #{mainId,jdbcType=INTEGER},
      main_name = #{mainName,jdbcType=VARCHAR},
      reject_name = #{rejectName,jdbcType=VARCHAR},
      assistant_aid = #{assistantAid,jdbcType=VARCHAR},
      assistant_name = #{assistantName,jdbcType=VARCHAR},
      public_again = #{publicAgain,jdbcType=INTEGER},
      audit_time = #{auditTime,jdbcType=TIMESTAMP},
      new_user = #{newUser,jdbcType=VARCHAR},
      sign_sum = #{signSum,jdbcType=VARCHAR},
      user_names = #{userNames,jdbcType=VARCHAR},
      object_type = #{objectType,jdbcType=INTEGER},
      public_type = #{publicType,jdbcType=INTEGER},
      district_name = #{districtName,jdbcType=VARCHAR},
      clock_in = #{clockIn,jdbcType=INTEGER}
    where id = #{id,jdbcType=INTEGER}
  </update>

  <update id="updateSignByAidAndUid">
    update public_release_details a left join public_release b on a.prid =b.id
      left join `user` c on a.uid=c.id
    set a.sign_sum =a.sign_sum +1
    where a.uid=  #{uid} and a.main_status =1 and b.aid=  #{aid}
  </update>


  <select id="listPublicRelease" resultType="com.goafanti.weChat.bo.OutPublicReleaseList">
    select  a.id ,c.name  aname,date_format(a.release_start,'%Y-%m-%d %H:%i:%S') releaseStarts,a.aid,
    date_format(a.release_end ,'%Y-%m-%d %H:%i:%S') releaseEnds,date_format(a.create_time ,'%Y-%m-%d %H:%i:%S') createTimes,a.status,
    date_format(a.update_time ,'%Y-%m-%d %H:%i:%S') updateTimes,a.update_status updateStatus,a.district_name districtName,
    a.assist,a.assist_aid assistAid,a.assist_aid_name assistAidName,a.type,a.main_name mainName,a.reject_name rejectName,
    a.assistant_aid assistantAid ,a.assistant_name assistantName,a.public_again publicAgain,a.user_names userNames,a.clock_in clockIn
    from public_release a  left join admin c on a.aid =c.id
    <if test="type==2">
      left join department d on c.department_id =d.id
    </if>
    <if test="type==4 or userName !=null">
    left join public_release_details b on a.id=b.prid
      left join user e on b.uid=e.id
    </if>
      where 1=1
    <if test="type==0">
      and a.public_type in (0,1,2,3)
      <if test="assist ==0">
        and a.assist in(0,1)
      </if>
      <if test="assist == 2">
        and a.assist    =2
      </if>
      <if test="assist == 3">
        and a.assist    =3
      </if>
      <if test="assist ==4">
        and a.assist in (2,3)
      </if>
      <if test="assistType ==1">
        and a.assist in(0,1,2)
      </if>
      <if test="aid !=null">
        and a.aid= #{aid}
      </if>
      <if test="statusType ==1">
        and a.status in (0,1,2,4)
      </if>
    </if>
    <if test="type==1">
      and a.public_type in (0,2)
      and a.assist in(0,1)
      and c.reviewer = #{aid}
    </if>
    <if test="type==2">
      and a.public_type in (0,2)
      and a.assist in(0,1)
      and d.manager_id = #{aid}
    </if>
    <if test="type==3">
      and a.public_type in (0,2)
      and a.assist=2
      <if test="aid !=null">
        and a.aid= #{aid}
      </if>
      and a.type = #{type}
    </if>
    <if test="type==4">
      and a.assist in(0,1)
      and e.aid = #{aid}
      and a.public_type in (1,2)
    </if>
    <if test="clockIn !=null">
      and a.clock_in =#{clockIn}
    </if>
    <if test="clockTime !=null">
      and a.release_start &lt; #{clockTime} and a.release_end &gt; #{clockTime}
    </if>
    <if test="releaseStarts != null and publicEnd != null">
      and a.release_start  BETWEEN #{releaseStarts} and #{publicEnd}
    </if>
    <if test="userName !=null">
      and e.nickname like concat('%',#{userName},'%')
    </if>
    <if test="status !=null">
      and a.status = #{status}
    </if>
    <if test="type==2 or type==4">
      ORDER BY a.create_time desc
    </if>
    <if test="type==0 or type==1 or type==3">
      ORDER BY (case when a.status=1 then 1 when a.status=2 then 2 when a.status=3 then 5
      when a.status=0 then 3 when a.status=4 then 4
      else 0 end),a.create_time desc
    </if>
  	<if test="page_sql!=null">
			${page_sql}
	</if>
  </select>
  <select id="countPublicRelease" resultType="java.lang.Integer">
  select  count(*)
	from public_release a  left join admin c on a.aid =c.id
    <if test="type==2">
      left join department d on c.department_id =d.id
    </if>
    <if test="type==4 or userName !=null">
      left join public_release_details b on a.id=b.prid
      left join user e on b.uid=e.id
    </if>
    where 1=1
    <if test="type==0">
      and a.public_type in (0,1,2,3)
      <if test="assist ==0">
        and a.assist in(0,1)
      </if>
      <if test="assist == 2">
        and a.assist    =2
      </if>
      <if test="assist == 3">
        and a.assist    =3
      </if>
      <if test="assist ==4">
        and a.assist in (2,3)
      </if>
      <if test="assistType ==1">
        and a.assist in(0,1,2)
      </if>
      <if test="aid !=null">
        and a.aid= #{aid}
      </if>
      <if test="statusType ==1">
        and a.status in (0,1,2,4)
      </if>
    </if>
    <if test="type==1">
      and a.public_type in (0,2)
      and a.assist in(0,1)
      and c.reviewer = #{aid}
    </if>
    <if test="type==2">
      and a.public_type in (0,2)
      and a.assist in(0,1)
      and d.manager_id = #{aid}
    </if>
    <if test="type==3">
      and a.public_type in (0,2)
      and a.assist=2
      <if test="aid !=null">
        and a.aid= #{aid}
      </if>
      and a.type = #{type}
    </if>
    <if test="type==4">
      and a.assist in(0,1)
      and e.aid = #{aid}
      and a.public_type in (1,2)
    </if>
    <if test="userName !=null">
      and e.nickname like concat('%',#{userName},'%')
    </if>
	<if test="clockTime !=null">
	and a.release_start &lt; #{clockTime} and a.release_end &gt; #{clockTime}
	</if>
	<if test="releaseStarts != null and publicEnd != null">
	and a.release_start  BETWEEN #{releaseStarts} and #{publicEnd}
	</if>

	<if test="status !=null">
	and a.status = #{status}
	</if>
  </select>

  <select id="selectDtails" resultType="com.goafanti.weChat.bo.OutPublicRelease">
    select  a.id,c.name  aname,date_format(a.release_start,'%Y-%m-%d %H:%i:%S') releaseStarts,a.update_time updateTime,a.user_names userNames,
    date_format(a.release_end ,'%Y-%m-%d %H:%i:%S') releaseEnds,date_format(a.create_time ,'%Y-%m-%d %H:%i:%S') createTimes,a.status,
    a.annex_url annexUrl ,a.remarks,a.duration ,a.valid_date validDate ,a.aid ,c.superior_id suprId,
    a.plan ,a.expected_effect expectedEffect,a.next_plan nextPlan,c.reviewer,a.type,a.order_no orderNo,d.contract_no contractNo,
    date_format(a.update_time ,'%Y-%m-%d %H:%i:%S') updateTimes,a.update_Status updateStatus,a.assist,a.assist_aid assistAid,a.assist_aid_name assistAidName,
    a.main_name mainName,a.assistant_aid assistantAid,a.assistant_name assistantName,a.public_again publicAhain,x.uids,a.public_type publicType
    from public_release a  left join admin c on a.aid =c.id
    left join (select prid ,GROUP_CONCAT(uid)uids from public_release_details where prid= #{id} group by prid)x on a.id=x.prid
    left join t_order_new d on a.order_no=d.order_no
    where 1=1
	<if test="id !=null">
	and a.id= #{id}
	</if>
  </select>
  <select id="selectDtailsByFid" resultType="com.goafanti.weChat.bo.OutPublicReleaseAndDetails">
    select  a.id,c.name  aname,date_format(a.release_start,'%Y-%m-%d %H:%i:%S') releaseStarts,a.update_time updateTime,a.user_names userNames,
    date_format(a.release_end ,'%Y-%m-%d %H:%i:%S') releaseEnds,date_format(a.create_time ,'%Y-%m-%d %H:%i:%S') createTimes,a.status,
    a.annex_url annexUrl ,a.remarks,a.duration ,a.valid_date validDate ,a.aid ,c.superior_id suprId,x.uid uids,x.district_name districtName ,u.nickname,
    a.plan ,a.expected_effect expectedEffect,a.next_plan nextPlan,c.reviewer,a.type,a.order_no orderNo,d.contract_no contractNo,
    date_format(a.update_time ,'%Y-%m-%d %H:%i:%S') updateTimes,a.update_Status updateStatus,a.assist,a.assist_aid assistAid,a.assist_aid_name assistAidName,
    a.main_name mainName,a.assistant_aid assistantAid,a.assistant_name assistantName,a.public_again publicAhain
    from public_release a  left join admin c on a.aid =c.id
    left join  public_release_details x on a.id=x.prid
    left join user u on x.uid=u.id
    left join t_order_new d on a.order_no=d.order_no
    where 1=1
    <if test="ufid !=null">
      and x.ufid= #{ufid}
    </if>
  </select>

  <select id="listPublicStatistics" resultType="com.goafanti.weChat.bo.OutPublicStatistics">
  	select y.aid,y.bh,y.dk,y.wsh,y.tg,y.duration,c.name from(select x.aid ,sum(x.bh)bh,sum(x.wsh)wsh,sum(x.tg)tg,sum(x.dk)dk,sum(x.duration)duration
	from (select a.aid, if(a.status=0,1,0)bh ,if(a.status=1,1,0)wsh,if(a.status=2,1,0)tg,b.dk ,if(a.status=2,a.duration,0)duration
    from public_release a inner join (select prid ,sum(if(clock_in=1,1,0))dk from public_release_details where 1=1
    <if test="clockStart != null and clockEnd != null">
      and clock_in_time  between #{clockStart} and #{clockEnd}
    </if>
    group by prid ) b on a.id =b.prid
    left join admin c on a.aid=c.id where 1=1
	<if test="aid != null">
	and a.aid= #{aid}
	</if>
	<if test="createStart != null and createEnd !=null">
	and a.create_time  between #{createStart} and #{createEnd}
	</if>
	<if test="depId != null">
	and c.department_id = #{depId}
	</if>
	)x  group by x.aid)y left join admin c on c.id=y.aid
	<if test="page_sql!=null">
			${page_sql}
	</if>
  </select>


  <select id="countPublicStatistics" resultType="java.lang.Integer">
  	select count(*) from (select aid from public_release a
    inner join (select prid ,sum(if(clock_in=1,1,0))dk from public_release_details where 1=1
    <if test="clockStart != null and clockEnd != null">
      and clock_in_time  between #{clockStart} and #{clockEnd}
    </if>
    group by prid ) b on a.id =b.prid
  	    left join admin c on a.aid=c.id
	where 1=1
	<if test="aid != null">
	and a.aid= #{aid}
	</if>
	<if test="createStart != null and createEnd !=null">
	and a.create_time  between #{createStart} and #{createEnd}
	</if>
	<if test="depId != null">
	and c.department_id = #{depId}
	</if>
	group by aid)x
  </select>

  <select id="listPublicDtails" resultType="com.goafanti.weChat.bo.OutPublicDtails">
    select b.id,a.district_name districtName ,c.name aname,a.duration ,b.status ,b.annex_url annexUrl,a.photo_url photoUrl,d.nickname userName,
    date_format(a.clock_in_time ,'%Y-%m-%d %H:%i:%S') clockInTimes,a.clock_in clockIn ,b.remarks ,b.supplement ,b.`type` ,b.object_type objectType ,
    date_format(b.create_time ,'%Y-%m-%d %H:%i:%S') createTimes,b.audit_info auditInfo,date_format(b.audit_time ,'%Y-%m-%d %H:%i:%S') auditTimes,
    date_format(b.release_start,'%Y-%m-%d %H:%i:%S') releaseStarts,date_format(b.release_end ,'%Y-%m-%d %H:%i:%S') releaseEnds,
    b.plan ,b.next_plan nextPlan ,b.expected_effect expectedEffect,b.update_status updateStatus,b.assist_aid_name assistAidName,
    b.main_name mainName,b.assist,b.assistant_aid assistantAid, b.assistant_name assistantName,f.contract_no contractNo,
    e.name sname,f.total_amount  totalAmout
    from public_release_details a left join public_release b on a.prid =b.id
    left join admin c on b.aid =c.id left join t_order_new f on b.order_no =f.order_no
    left join `user` d on a.uid =d.id  left join admin e on d.aid=e.id
    where 1=1
    <if test="publicType != null">
      and b.type= #{publicType}
    </if>
    <if test="aid != null">
      and b.aid= #{aid}
    </if>
    <if test="sid != null">
      and d.aid= #{sid}
    </if>
    <if test="deps !=null">
       and c.department_id in
      <foreach close=")" collection="deps" item="dep" open="(" separator=",">
        #{dep}
      </foreach>
    </if>
	<if test="uid != null">
	and a.uid = #{uid}
	</if>
	<if test="releaseStart != null and releaseEnd != null">
	and (b.release_start  between #{releaseStart} and #{releaseEnd} or b.release_end  between #{releaseStart} and #{releaseEnd})
	</if>
    <if test="createStart != null and createEnd != null">
      and b.create_time  between #{createStart} and #{createEnd}
    </if>
	<if test="status != null">
	and b.status = #{status}
	</if>
	<if test="clockIn !=null">
	and a.clock_in= #{clockIn}
	</if>
    <if test="contractNo !=null">
    and f.contract_no like CONCAT('%',#{contractNo},'%')
    </if>
    order by b.status,b.create_time desc
	<if test="page_sql!=null">
			${page_sql}
	</if>
  </select>

  <select id="countPublicDtails" resultType="java.lang.Integer">
  select count(*)
    from public_release_details a left join public_release b on a.prid =b.id
    left join admin c on b.aid =c.id left join `user` d on a.uid =d.id
    <if test="contractNo !=null">
    left join t_order_new f on b.order_no =f.order_no
    </if>
    where 1=1
    <if test="publicType != null">
      and b.type= #{publicType}
    </if>
    <if test="aid != null">
      and b.aid= #{aid}
    </if>
    <if test="sid != null">
      and d.aid= #{sid}
    </if>
    <if test="deps !=null">
      and c.department_id in
      <foreach close=")" collection="deps" item="dep" open="(" separator=",">
        #{dep}
      </foreach>
    </if>
    <if test="uid != null">
      and a.uid = #{uid}
    </if>
    <if test="releaseStart != null and releaseEnd != null">
      and (b.release_start  between #{releaseStart} and #{releaseEnd} or b.release_end  between #{releaseStart} and #{releaseEnd})
    </if>
    <if test="createStart != null and createEnd != null">
      and b.create_time  between #{createStart} and #{createEnd}
    </if>
    <if test="status != null">
      and b.status = #{status}
    </if>
    <if test="clockIn !=null">
      and a.clock_in= #{clockIn}
    </if>
    <if test="contractNo !=null">
      and f.contract_no like CONCAT('%',#{contractNo},'%')
    </if>
  </select>

  <select id="checkTime" resultType="java.lang.Integer">
 select id from public_release where aid=#{aid} and status in (0,1,2)
 and ( (release_start &gt;= #{start} and release_start &lt; #{end})
  or  (release_start &lt; #{start} and release_end &gt; #{start})
  or  (release_start &lt; #{end} and release_end &gt; #{end}))
  </select>
  <select id="getTimeUserFollow" resultType="com.goafanti.common.model.OutPublicReleaseCount">
    select e.id province,d.id depId,a.aid,a.uid,a.new_user newUser ,1 quantity from (select a.aid,b.uid,b.new_user from public_release a
        left join public_release_details b on a.id=b.prid
     where b.main_status =1 and a.create_time  between #{date} and #{now}
        group by a.aid,b.uid,b.new_user)a left join admin c on a.aid=c.id
    left join department d on c.department_id =d.id left join district_glossory e on d.province =e.id
  </select>
  <select id="publicByOrder" resultType="com.goafanti.weChat.bo.OutPublicRelease">
    select a.id,a.uid,b.aid,b.nickname ,c.name aname,a.duration ,a.district_name districtName,
           date_format(pr.release_start,'%Y-%m-%d %H:%i:%S')releaseStarts,
           date_format(pr.release_end ,'%Y-%m-%d %H:%i:%S')releaseEnds,
           date_format(a.clock_in_time ,'%Y-%m-%d %H:%i:%S')clockInTimes,
           pr.remarks
    from public_release_details a left join public_release pr on a.prid=pr.id
        left join user b on a.uid=b.id left join admin c on pr.aid =c.id
    where pr.order_no = #{orderNo}
    </select>
  <select id="selectByMainId" parameterType="java.lang.Integer" resultMap="BaseResultMap">
    select
    <include refid="Base_Column_List" />
    from public_release
    where main_id = #{mainId}
    <if test="type !=null">
      and type= #{type}
    </if>
    <if test="aid !=null">
      and aid= #{aid}
    </if>
  </select>
  <update id="updateStatusByMainId">
    update public_release set status= #{status}
    where main_id= #{mainId}
    <if test="type !=null">
      and type= #{type}
    </if>
    <if test="aid !=null">
      and aid= #{aid}
    </if>
  </update>
</mapper>
