<?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.huimv.receive.mapper.SysUserMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.huimv.receive.entity.SysUser">
        <id column="id" property="id" />
        <result column="user_id" property="userId" />
        <result column="user_name" property="userName" />
        <result column="male" property="male" />
        <result column="nation" property="nation" />
        <result column="native_place" property="nativePlace" />
        <result column="birthday" property="birthday" />
        <result column="politics_pace" property="politicsPace" />
        <result column="people_type" property="peopleType" />
        <result column="educational_level" property="educationalLevel" />
        <result column="card_type" property="cardType" />
        <result column="age" property="age" />
        <result column="card_num" property="cardNum" />
        <result column="user_phone" property="userPhone" />
        <result column="public_phone" property="publicPhone" />
        <result column="user_mali" property="userMali" />
        <result column="public_mali" property="publicMali" />
        <result column="entry_time" property="entryTime" />
        <result column="probation" property="probation" />
        <result column="department" property="department" />
        <result column="job" property="job" />
        <result column="job_type" property="jobType" />
        <result column="work_location" property="workLocation" />
        <result column="work_location_type" property="workLocationType" />
    </resultMap>
    <select id="pageAccount" resultType="com.huimv.receive.entity.vo.UserAccountVo">
SELECT sys_user.`user_id`,sys_user.`id`,sys_user.`user_name`,sys_user.`job`, sys_user.`work_location`, sys_user.`work_location_type` ,
	sys_account_multilevel.`account`,sys_account_multilevel.`password`
FROM `sys_account_multilevel` INNER JOIN   `sys_user`   ON `sys_user`.id = `sys_account_multilevel`.user_id
   ${ew.customSqlSegment} AND`sys_account_multilevel`.farm_id = #{farmId}  and  `sys_account_multilevel`.user_id  IS NOT NULL
 ORDER  BY sys_account_multilevel.id DESC
    </select>
    <select id="userAgeAndGender" resultType="com.huimv.receive.entity.vo.UserAgeAndGender">

SELECT ar.`age_range` ,IFNULL(a.male_count,0) male_count ,IFNULL(a.female_count ,0) female_count,IFNULL(a.total_count,0) total_count FROM `age_ranges` ar LEFT JOIN
(SELECT
    age_range,
    SUM(CASE WHEN gender = 0 THEN 1 ELSE 0 END) AS male_count,
    SUM(CASE WHEN gender = 1 THEN 1 ELSE 0 END) AS female_count,
    COUNT(*) AS total_count
FROM (
    SELECT
        CASE
	        WHEN age BETWEEN 18 AND 30 THEN '18-30'
            WHEN age BETWEEN 30 AND 40 THEN '30-40'
            WHEN age BETWEEN 40 AND 50 THEN '40-50'
            WHEN age BETWEEN 50 AND 60 THEN '50-60'
            WHEN age >= 60  THEN '60+'
        END AS age_range,
        gender
    FROM `sys_user`  WHERE farm_id= #{farmId}
) AS age_gender
GROUP BY age_range
) a ON ar.age_range = a.age_range  ORDER BY ar.`age_range`
    </select>

    <select id="userJob" resultType="com.huimv.receive.entity.vo.UserJob">

      SELECT
        IFNULL (SUM(CASE WHEN job LIKE "%5%"  THEN 1 ELSE 0 END) ,0)AS clean,
        IFNULL (SUM(CASE WHEN job LIKE "%4%"  THEN 1 ELSE 0 END) ,0) AS pcr,
        IFNULL (SUM(CASE WHEN job LIKE "%3%"  THEN 1 ELSE 0 END)  ,0)AS safe,
       IFNULL ( SUM(CASE WHEN job LIKE "%1%"  THEN 1 ELSE 0 END)  ,0)AS factory
    FROM
       `sys_user`
       WHERE farm_id = #{farmId}

    </select>

</mapper>
