<?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.wise.sc.cement.business.mapper.DataStatisticsMapper">

    <select id="countTeamByTime" resultType="cn.wise.sc.cement.business.model.vo.DataStatisticsVo">
        SELECT  DATE_FORMAT(sd.create_time,'%Y-%m') as name, count(sd.id) as value
        FROM sample_distribution sd
        where sd.finish_time is not null
        <if test="params.cycle == 1 ">
            and YEARWEEK(date_format(sd.create_time,'%Y-%m-%d' ) ) = YEARWEEK(now())
        </if>
        <if test="params.cycle == 2 ">
            and DATE_FORMAT( sd.create_time, '%Y%m' ) = DATE_FORMAT( CURDATE( ) ,'%Y%m')
        </if>
        <if test="params.cycle = 3 ">
            and YEAR(sd.create_time) = YEAR( NOW( ) )
        </if>
        <if test="params.startDate != null and params.startDate != ''">
            and DATE(sd.create_time) &gt;= #{params.startDate}
        </if>
        <if test="params.endDate != null and params.endDate != ''">
            and DATE(sd.create_time) &lt;= #{params.endDate}
        </if>
        group by name
    </select>

    <select id="countTeamByOrigin" resultType="cn.wise.sc.cement.business.model.vo.DataStatisticsVo">
        SELECT s.origin as name, count(sd.id) as value
        FROM sample_distribution sd
        left join sample s on s.id = sd.sample_id
        where sd.finish_time is not null
        <if test="params.cycle == 1 ">
            and YEARWEEK(date_format(sd.create_time,'%Y-%m-%d' ) ) = YEARWEEK(now())
        </if>
        <if test="params.cycle == 2 ">
            and DATE_FORMAT( sd.create_time, '%Y%m' ) = DATE_FORMAT( CURDATE( ) ,'%Y%m')
        </if>
        <if test="params.cycle == 3 ">
            and YEAR(sd.create_time) = YEAR( NOW( ) )
        </if>
        <if test="params.endDate != null and params.endDate != ''">
            and DATE(sd.create_time) &lt;= #{params.endDate}
        </if>
        <if test="params.endDate != null and params.endDate != ''">
            and DATE(sd.create_time) &lt;= #{params.endDate}
        </if>
        group by s.origin
    </select>


    <select id="countEntrustStatus" resultType="cn.wise.sc.cement.business.model.vo.DataStatisticsVo">
        SELECT (
        CASE e.status
        WHEN 0 THEN '未评审'
        WHEN 1 THEN '已通过'
        WHEN 2 THEN '未通过'
        WHEN 3 THEN '样品处理中'
        WHEN 4 THEN '样品处理完成'
        WHEN 5 THEN '样品检测中'
        WHEN 6 THEN '样品检测完成'
        WHEN 7 THEN '校核中'
        WHEN 8 THEN '校核完成'
        ELSE ''
        END
        ) as name,
		count(*) as value
        FROM entrust e
        where e.status != 0 and e.project_type is not null
        <if test="params.cycle == 1 ">
            and YEARWEEK(date_format(e.create_time,'%Y-%m-%d' ) ) = YEARWEEK(now())
        </if>
        <if test="params.cycle == 2 ">
            and DATE_FORMAT( e.create_time, '%Y%m' ) = DATE_FORMAT( CURDATE( ) ,'%Y%m')
        </if>
        <if test="params.cycle == 3 ">
            and YEAR(e.create_time) = YEAR( NOW( ) )
        </if>
        <if test="params.endDate != null and params.endDate != ''">
            and DATE(e.create_time) &lt;= #{params.endDate}
        </if>
        <if test="params.endDate != null and params.endDate != ''">
            and DATE(e.create_time) &lt;= #{params.endDate}
        </if>
		group by e.status
    </select>

    <select id="countEntrustType" resultType="cn.wise.sc.cement.business.model.vo.DataStatisticsVo">
       select e.project_type, count(*)
        from entrust e
        where e.status != 0 and e.project_type is not null
        <if test="params.cycle == 1 ">
            and YEARWEEK(date_format(e.create_time,'%Y-%m-%d' ) ) = YEARWEEK(now())
        </if>
        <if test="params.cycle == 2 ">
            and DATE_FORMAT( e.create_time, '%Y%m' ) = DATE_FORMAT( CURDATE( ) ,'%Y%m')
        </if>
        <if test="params.cycle == 3 ">
            and YEAR(e.create_time) = YEAR( NOW( ) )
        </if>
        <if test="params.endDate != null and params.endDate != ''">
            and DATE(e.create_time) &lt;= #{params.endDate}
        </if>
        <if test="params.endDate != null and params.endDate != ''">
            and DATE(e.create_time) &lt;= #{params.endDate}
        </if>
        group by e.project_type
    </select>

    <select id="countEntrustPage" resultType="cn.wise.sc.cement.business.model.vo.EntrustVo">
        select e.*, c.name as clientName, su.name as userName,
        (
        CASE e.status
        WHEN 0 THEN '未评审'
        WHEN 1 THEN '已通过'
        WHEN 2 THEN '未通过'
        WHEN 3 THEN '样品处理中'
        WHEN 4 THEN '样品处理完成'
        WHEN 5 THEN '样品检测中'
        WHEN 6 THEN '样品检测完成'
        WHEN 7 THEN '校核中'
        WHEN 8 THEN '校核完成'
        ELSE ''
        END
        ) as statusValue
        from entrust e
        left join project p on p.id = e.project_id
        left join client c on c.id = e.client_id
        left join sys_user su on su.id = e.user_id
        where e.status != 0 and e.project_type is not null
        order by create_time desc
    </select>






    <select id="countHandlePage" resultType="cn.wise.sc.cement.business.model.vo.DataStatisticsVo">
       SELECT su.name as name, count(*) as value FROM sample_handle sh
        left join sys_user su on su.id = sh.user_id
        where sh.status != 2
        group by sh.user_id
    </select>

    <select id="countDistributionPage" resultType="cn.wise.sc.cement.business.model.vo.DataStatisticsVo">
       SELECT su.name as name, count(*) as value FROM sample_distribution sd
        left join sys_user su on su.id = sd.user_id
        where sd.status != 2
        group by sd.user_id
    </select>

    <select id="countTeamIng" resultType="cn.wise.sc.cement.business.model.vo.DataStatisticsVo">
       SELECT t.name as name, count(*) as value FROM sample_distribution sd
        left join team t on t.id = sd.team_id
        where sd.status != 2
        group by sd.team_id
    </select>

    <select id="countTeam" resultType="cn.wise.sc.cement.business.model.vo.DataStatisticsVo">
       SELECT t.name as name, count(*) as value FROM sample_distribution sd
        left join team t on t.id = sd.team_id
        where 1=1
        <if test="params.cycle == 1 ">
            and YEARWEEK(date_format(sd.create_time,'%Y-%m-%d' ) ) = YEARWEEK(now())
        </if>
        <if test="params.cycle == 2 ">
            and DATE_FORMAT( sd.create_time, '%Y%m' ) = DATE_FORMAT( CURDATE( ) ,'%Y%m')
        </if>
        <if test="params.cycle == 3 ">
            and YEAR(sd.create_time) = YEAR( NOW( ) )
        </if>
        <if test="params.endDate != null and params.endDate != ''">
            and DATE(sd.create_time) &lt;= #{params.endDate}
        </if>
        <if test="params.endDate != null and params.endDate != ''">
            and DATE(sd.create_time) &lt;= #{params.endDate}
        </if>
        group by sd.team_id
    </select>

    <select id="countTeamList" resultType="cn.wise.sc.cement.business.model.vo.DataStatisticsTeamVo">
        SELECT DISTINCT t.name as teamName, e.project_code as projectCode,
        e.entrust_code as entrustCode, e.entrust_date as entrustDate,
        e.sample_num as sampleNum
        FROM sample_distribution sd
        left join sample s on s.id = sd.sample_id
        left join entrust e on e.id = s.entrust_id
        left join team t on t.id = sd.team_id
    </select>







    <select id="countItemDistributionSample"
            resultType="cn.wise.sc.cement.business.entity.ItemDistributionSample">
        SELECT COUNT(sd.team_id) as `count`,t.`name` FROM sample_distribution sd
        LEFT JOIN sample s ON sd.sample_id = s.id
        LEFT JOIN team t ON t.id = sd.team_id
        WHERE
        sd.finish_time BETWEEN #{start} AND #{end}
        <if test="origin != null and origin != '' ">
            AND s.origin = #{origin}
        </if>
        <if test="teamId != '' and teamId != null ">
            AND sd.team_id = #{teamId}
        </if>
    </select>

    <select id="countItemDistribution" resultType="cn.wise.sc.cement.business.entity.ItemDistribution">
        SELECT COUNT(a.sample_id) as `count`,`key` from
        (SELECT sd.team_id,sd.sample_id,t.`name`,sd.finish_time,CONCAT(sd.team_id,',',s.origin,',',s.is_foreign) as
        `key` FROM sample_distribution sd
        LEFT JOIN sample s ON sd.sample_id = s.id
        LEFT JOIN team t ON t.id = sd.team_id
        WHERE (sd.finish_time between #{start} and #{end})
        <if test="origins != null  and origins.size()>0">
            AND s.origin in
            <foreach collection="origins" item="item" open="(" separator="," close=")">
                #{item}
            </foreach>
        </if>
        <if test="itemId != null">
            AND sd.team_id = #{itemId}
        </if>
        ) a GROUP BY a.`key`
    </select>





</mapper>