<?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="cn.com.countrygarden.bi.main.dao.canteen.CanteenInvestigateMapper">
    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="cn.com.countrygarden.bi.main.model.canteen.CanteenInvestigate">
                                    <id column="id" property="id" />
                                                <result column="title" property="title" />
                                                <result column="area_id" property="areaId" />
                                                <result column="project_id" property="projectId" />
                                                <result column="questionnaire_id" property="questionnaireId" />
                                                <result column="flag" property="flag" />
                                                <result column="create_by" property="createBy" />
                                                <result column="create_time" property="createTime" />
                                                <result column="update_by" property="updateBy" />
                                                <result column="update_time" property="updateTime" />
                        </resultMap>

      <select id="canteenrqdtolist" resultType="cn.com.countrygarden.bi.main.dto.ReviewQuestionnairelistCanteenDTO">
             select q.id id,q.name name ,q.flag flag ,(select count(1) from canteen_investigate ci
		where ci.questionnaire_id=q.id) total,(select count(distinct(ci.questionnaire_id)) from canteen_investigate ci
		where ci.questionnaire_id=q.id and ci.is_partake=1) underWay from 
		review_questionnaire q
		where q.flag=1 
		<if test="name!=null and name!=''">
			and q.name like concat('%',#{name},'%')
		</if>
		<if test="investigationmanagementId!=null">
			and q.investigationmanagement_id =#{investigationmanagementId}
		</if>
		<if test="hasRole==false">
			and q.create_by=#{userCode}
		</if>
		order by q.create_time desc
      </select>

      <select id="canteentionliebli" parameterType="cn.com.countrygarden.bi.main.dto.canteenInvestigationliebiaoDTO" 
        resultType="cn.com.countrygarden.bi.main.dto.canteenInvestigationliebiaoDTO">
      
         select ci.id as id,ci.project_id as projectId,rq.id as questionnaireId,ci.areaname as areaname,ci.projectname as projectname,ci.title as title,rq.name as questionnaireName,rq.investigationmanagement_id as investigationmanagementId,ci.is_partake AS ispartake,
          (select count(distinct cs.staff_id) from canteen_score cs
          where rq.id=ci.questionnaire_id and cs.investigate_id=ci.id) as total,rq.flag 
          as flag,rq.create_time as createTime from review_questionnaire rq,canteen_investigate ci
          where ci.questionnaire_id=rq.id and rq.flag =1         
                 <if test="investigationmanagementId!=null and investigationmanagementId!=''">
			        and rq.investigationmanagement_id=#{investigationmanagementId}
		         </if>

                 <if test="questionnaireName!=null and questionnaireName!=''">
                      and rq.name like concat('%',#{questionnaireName},'%')
                 </if>
                 <if test="haseole==false">
			         and rq.create_by=#{getusercode}
		         </if>
           
         
            order by ci.create_time desc  

     </select>

            
     <update id="updatecandiaoch" parameterType="cn.com.countrygarden.bi.main.model.canteen.CanteenInvestigate">
          
       UPDATE canteen_investigate SET is_partake=#{ispartake},update_by=#{updateBy},update_time=#{updateTime}	
	   WHERE id=#{id};
     
     </update>

     <select id="canteenInvesdati" resultType="cn.com.countrygarden.bi.main.dto.canteenInvestigationliebiaoDTO">


                select rq.id as questionnaireId,rq.name as questionnaireName,rq.described as described,ci.id as cantigateid,ci.title as title,ci.is_partake as ispartake from 
                canteen_investigate ci,review_questionnaire rq
               where ci.questionnaire_id=rq.id and ci.questionnaire_id=#{questionnaireId} 
               and ci.project_id=#{projectid} and ci.id=#{canteeninsdid} and rq.investigationmanagement_id=#{investigatagementid}

     </select>

     <select id="problemVo" resultType="cn.com.countrygarden.bi.main.dto.ReviewQuestionnaireProblemVo">
                 select rqp.id as id,rqpc.name AS `name`,rqp.title_description as title,rqp.questionnaire_problem_classification_id as classification,rqp.question_type_id as type,rqpt.orderxu as orderxu,rqp.score as score
                 from review_questionnaire_problem rqp
                 left join review_questionnaire rq on rq.id=rqp.questionnaire_id
                 left join review_questionnaire_problem_type rqpt on rqpt.id=rqp.question_type_id
                 left join review_questionnaire_problem_classification rqpc on rqpc.id=rqp.questionnaire_problem_classification_id
                 where rq.investigationmanagement_id=#{investigatagementid} and rq.id=#{questionnaireId} AND rqp.question_type_id <![CDATA[<>]]>2 AND rqp.question_type_id <![CDATA[<>]]>4
                  order by classification 

                  
     </select>

     <select id="option" resultType="cn.com.countrygarden.bi.main.dto.ReviewQuestionnaireProblemOptionVo">
            select rqpo.id as optionId,rqpo.questionnaire_problem_id as problemId,rqpo.name as name,rqpo.option_score as optionScore
            from review_questionnaire_problem_option rqpo
            where rqpo.questionnaire_problem_id=#{problemid}  

     </select>

     <update id="updateorgancode">
            update canteen_investigate set areaname=#{areaname} WHERE id=#{id};
     </update>

     <update id="updatefindcomm">

         update canteen_investigate set projectname=#{projectname} WHERE id=#{id};

     </update>

     <select id="problemjianweVo" resultType="cn.com.countrygarden.bi.main.dto.ReviewQuestionnaireProblemVo">
             select rqp.id as id,rqpc.name AS `name`,rqp.title_description as title,rqp.questionnaire_problem_classification_id as classification,rqp.question_type_id as type,rqpt.orderxu as orderxu,rqp.score as score
                 from review_questionnaire_problem rqp
                 left join review_questionnaire rq on rq.id=rqp.questionnaire_id
                 left join review_questionnaire_problem_type rqpt on rqpt.id=rqp.question_type_id
                 left join review_questionnaire_problem_classification rqpc on rqpc.id=rqp.questionnaire_problem_classification_id
                 where rq.investigationmanagement_id=#{investigatagementid} and rq.id=#{questionnaireId} AND rqp.question_type_id <![CDATA[<>]]>1 AND rqp.question_type_id <![CDATA[<>]]>3
                  order by `type` desc
     </select>
    

</mapper>