<?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.dayou.mapper.MajorProductionAllotMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.dayou.entity.MajorProductionAllot">
        <result column="id" property="id" />
        <result column="deleted" property="deleted" />
        <result column="created" property="created" />
        <result column="modified" property="modified" />
        <result column="major_production_id" property="majorProductionId" />
        <result column="user_id" property="userId" />
        <result column="ratio" property="ratio" />
        <result column="is_principal" property="isPrincipal" />
    </resultMap>

    <!-- 通用查询结果列 -->
    <sql id="Base_Column_List">
        id,
        deleted,
        created,
        modified,
        major_production_id, user_id, ratio, is_principal
    </sql>

    <select id="getList" parameterType="java.lang.Long" resultType="com.dayou.entity.MajorProductionAllot">
        SELECT
            mpa.id,
            mpa.major_production_id,
            mpa.user_id,
            mpa.ratio,
            mpa.is_principal,
            u.NAME AS userName
        FROM
            major_production_allot mpa
                LEFT JOIN user u ON u.id = mpa.user_id
        WHERE
            mpa.deleted = 0 and mpa.major_production_id = #{productionId} AND mpa.user_type='EVALUATE'
        ORDER BY
            mpa.is_principal DESC
    </select>

    <select id="findMembers" parameterType="java.lang.Long" resultType="com.dayou.entity.Major">
        SELECT
            principal_id , members from major where id = (select major_id from major_production where id = #{productionId})
    </select>

    <select id="blankAllot" parameterType="java.util.List" resultType="com.dayou.entity.MajorProductionAllot">
        SELECT name as userName,id as userId from user where id in (
            <foreach collection="members" item="item" index="index" separator=",">
                #{item}
            </foreach>
            )
    </select>

    <delete id="deleteByMajorProductionId" parameterType="java.lang.Long">
        delete from major_production_allot where major_production_id = #{productionId} and user_type = 'EVALUATE'
    </delete>

    <select id="allotRateByDeclareId" parameterType="java.lang.Long" resultType="com.dayou.dto.UserShareRate">
        select mpa.ratio as rate,mpa.user_id,u.name from major_production_allot mpa left join user u on u.id = mpa.user_id
        where mpa.declare_id = #{declareId} and mpa.deleted = 0 and mpa.user_type = 'MARKET'
    </select>

    <select id="getXList" parameterType="java.lang.Long" resultType="com.dayou.dto.UserShareRate">
        select mpa.ratio as rate,mpa.user_id,u.name from major_production_allot mpa left join user u on u.id = mpa.user_id
        where mpa.major_production_id = #{productionId} and mpa.deleted = 0 and mpa.user_type = 'EVALUATE'
    </select>
</mapper>
