<?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.ym.mec.biz.dal.dao.StudentGoodsSellDao">
	
	<resultMap type="com.ym.mec.biz.dal.entity.StudentGoodsSell" id="StudentGoodsSell">
		<result column="id_" property="id" />
		<result column="user_id_" property="userId" />
		<result column="teacher_id_" property="teacherId" />
		<result column="goods_json_" property="goodsJson" />
		<result column="total_amount_" property="totalAmount" />
		<result column="market_amount_" property="marketAmount" />
		<result column="coupon_market_amount_" property="couponMarketAmount" />
		<result column="order_no_" property="orderNo" />
		<result column="username_" property="userName" />
		<result column="organ_id_" property="organId" />
		<result column="author_user_" property="authorUser" />
		<result column="cooperation_organ_id_" property="cooperationOrganId" />
		<result column="create_time_" property="createTime" />
		<result column="update_time_" property="updateTime" />
		<result column="coupon_ids_" property="couponIds" />
        <result column="tenant_id_" property="tenantId"/>
	</resultMap>
	
	<!-- 根据主键查询一条记录 -->
	<select id="get" resultMap="StudentGoodsSell" >
		SELECT sgs.*,su.username_ FROM student_goods_sell sgs
		LEFT JOIN sys_user su ON su.id_ = sgs.user_id_
		WHERE sgs.id_ = #{id}
	</select>
	
	<!-- 全查询 -->
	<select id="findAll" resultMap="StudentGoodsSell">
		SELECT * FROM student_goods_sell where tenant_id_ = #{tenantId} ORDER BY id_
	</select>

	<!-- 向数据库增加一条记录 -->
	<insert id="insert" parameterType="com.ym.mec.biz.dal.entity.StudentGoodsSell" useGeneratedKeys="true" keyColumn="id" keyProperty="id">
		INSERT INTO student_goods_sell (user_id_,teacher_id_,goods_json_,total_amount_,market_amount_,
		create_time_,update_time_,order_no_,organ_id_,author_user_,cooperation_organ_id_,coupon_market_amount_,coupon_ids_,tenant_id_,goods_type_)
		VALUES(#{userId},#{teacherId},#{goodsJson},#{totalAmount},#{marketAmount},
		NOW(),NOW(),#{orderNo},#{organId},#{authorUser},#{cooperationOrganId},#{couponMarketAmount},#{couponIds},#{tenantId},#{goodsType})
	</insert>
	
	<!-- 根据主键查询一条记录 -->
	<update id="update" parameterType="com.ym.mec.biz.dal.entity.StudentGoodsSell">
		UPDATE student_goods_sell <set>
		<if test="couponIds != null">
			coupon_ids_ = #{couponIds},
		</if>
		<if test="couponMarketAmount != null">
			coupon_market_amount_ = #{couponMarketAmount},
		</if>
		<if test="cooperationOrganId != null">
			cooperation_organ_id_ = #{cooperationOrganId},
		</if>
		<if test="authorUser != null">
			author_user_ = #{authorUser},
		</if>
		<if test="organId != null">
			organ_id_ = #{organId},
		</if>
		<if test="orderNo != null">
			order_no_ = #{orderNo},
		</if>
		<if test="userId != null">
			user_id_ = #{userId},
		</if>
		<if test="totalAmount != null">
			total_amount_ = #{totalAmount},
		</if>
		<if test="marketAmount != null">
			market_amount_ = #{marketAmount},
		</if>
		<if test="teacherId != null">
			teacher_id_ = #{teacherId},
		</if>
		<if test="goodsJson != null">
			goods_json_ = #{goodsJson},
		</if>
		<if test="goodsType != null">
			goods_type_ = #{goodsType},
		</if>
		update_time_ = NOW()
		</set> WHERE id_ = #{id} and tenant_id_ = #{tenantId}
	</update>
	<update id="autoAffirmReceive">
		UPDATE student_payment_order SET receive_status_ = #{status},update_time_ = NOW() WHERE FIND_IN_SET(order_no_,#{orderNo})
	</update>

	<!-- 根据主键删除一条记录 -->
	<delete id="delete" >
		DELETE FROM student_goods_sell WHERE id_ = #{id} 
	</delete>
	
	<!-- 分页查询 -->
	<select id="queryPage" resultMap="StudentGoodsSell" parameterType="map">
		SELECT * FROM student_goods_sell where tenant_id_ = #{tenantId} ORDER BY id_ <include refid="global.limit"/>
	</select>
	
	<!-- 查询当前表的总记录数 -->
	<select id="queryCount" resultType="int">
		SELECT COUNT(*) FROM student_goods_sell where tenant_id_ = #{tenantId}
	</select>
    <select id="countStudentGoodsOrders" resultType="java.lang.Integer">
		SELECT COUNT(spo.id_) FROM student_payment_order spo
		LEFT JOIN student_goods_sell sgs ON spo.order_no_ = sgs.order_no_
		LEFT JOIN sys_user su ON sgs.user_id_ = su.id_
		<include refid="queryStudentGoodsOrdersSql"/>
	</select>
	<resultMap type="com.ym.mec.biz.dal.dto.StudentGoodsSellDto" id="StudentGoodsSellDto" extends="com.ym.mec.biz.dal.dao.StudentPaymentOrderDao.StudentPaymentOrder">
		<result column="goods_json_" property="goodsJson" />
		<result column="goods_type_" property="goodsType" />
		<result column="total_amount_" property="totalAmount" />
		<result column="market_amount_" property="marketAmount" />
		<result column="username_" property="userName" />
		<result column="author_user_" property="authorUser" />
	</resultMap>
	<sql id="queryStudentGoodsOrdersSql">
		<where>
			sgs.tenant_id_ = #{tenantId}

			<choose>
				<when test="goodsType != null and goodsType =='MALL'">
					and sgs.goods_type_ = 'MALL'
				</when>
				<otherwise>
					and sgs.goods_type_ is null
				</otherwise>
			</choose>
			<if test="teacherId != null">
				AND sgs.teacher_id_ = #{teacherId}
			</if>
			<if test="studentId != null">
				AND sgs.user_id_ = #{studentId}
			</if>
			<if test="status != null">
				AND spo.status_ = #{status}
			</if>
			<if test="receiveStatus != null and receiveStatus != ''">
				AND spo.receive_status_ = #{receiveStatus}
			</if>
			<if test="search != null and search != ''">
				AND (su.username_ LIKE CONCAT('%',#{search},'%') OR su.phone_ LIKE CONCAT('%',#{search},'%'))
			</if>
		</where>
	</sql>
	<select id="queryStudentGoodsOrders" resultMap="StudentGoodsSellDto">
		SELECT spo.*,sgs.goods_json_,sgs.total_amount_,sgs.market_amount_,su.username_,sgs.author_user_,sgs.goods_type_
		FROM student_payment_order spo
		LEFT JOIN student_goods_sell sgs ON spo.order_no_ = sgs.order_no_
		LEFT JOIN sys_user su ON sgs.user_id_ = su.id_
		<include refid="queryStudentGoodsOrdersSql"/>
		ORDER BY spo.create_time_ DESC
		<include refid="global.limit"/>
	</select>
	<select id="findByOrderNo" resultMap="StudentGoodsSell">
		SELECT * FROM student_goods_sell WHERE order_no_ = #{orderNo} LIMIT 1
	</select>
	<select id="getStudentGoodsSellDto" resultMap="StudentGoodsSellDto">
		SELECT spo.*,sgs.goods_json_,sgs.total_amount_,sgs.market_amount_ FROM student_payment_order spo
		LEFT JOIN student_goods_sell sgs ON spo.order_no_ = sgs.order_no_
		WHERE spo.order_no_ = #{orderNo} LIMIT 1
	</select>
    <select id="queryNoAffirmOrderNo" resultType="java.lang.String">
		SELECT GROUP_CONCAT(DISTINCT so.order_no_) FROM sell_order so
		LEFT JOIN student_payment_order spo ON spo.order_no_ = so.order_no_
		WHERE spo.status_ = 'SUCCESS' AND spo.receive_status_ = 'NO_RECEIVE' and spo.tenant_id_ = #{tenantId}
		<if test="autoAffirmReceiveTime != null and autoAffirmReceiveTime != ''">
			AND TIMESTAMPDIFF(DAY,spo.pay_time_,NOW()) >= #{autoAffirmReceiveTime}
		</if>
	</select>
	<resultMap type="com.ym.mec.biz.dal.dto.StudentPaymentOrderDto" id="StudentPaymentOrderDto" extends="com.ym.mec.biz.dal.dao.StudentPaymentOrderDao.StudentPaymentOrder">
		<result column="goods_image_" property="image" />
		<result column="goods_name_" property="goodsName" />
		<result column="goods_num_" property="goodsNum" />
		<result column="goods_amount_" property="goodsAmount" />
	</resultMap>
	<sql id="queryStudentPaymentOrdersSql">
		<where>
				spo.type_ IN ('GOODS_SELL','APPLY','SUBJECT_CHANGE') AND so.id_ IS NOT NULL and spo.tenant_id_ = #{tenantId}
			<if test="status != null">
				AND spo.status_ = #{status}
			</if>
			<if test="receiveStatus != null and receiveStatus != ''">
				AND spo.receive_status_ = #{receiveStatus}
			</if>
			<if test="studentId != null">
				AND spo.user_id_ = #{studentId}
			</if>
		</where>
	</sql>
    <select id="queryStudentPaymentOrders" resultMap="StudentPaymentOrderDto">
		SELECT spo.*,g.image_ goods_image_,g.name_ goods_name_,so.num_ goods_num_,so.expect_amount_ goods_amount_
		FROM student_payment_order spo
		LEFT JOIN sell_order so ON so.order_no_ = spo.order_no_
		LEFT JOIN goods g ON g.id_ = so.goods_id_
		<include refid="queryStudentPaymentOrdersSql"/>
		ORDER BY spo.pay_time_ DESC
		<include refid="global.limit"/>
	</select>
	<select id="countStudentPaymentOrders" resultType="java.lang.Integer">
		SELECT COUNT(DISTINCT spo.id_)
		FROM student_payment_order spo
		LEFT JOIN sell_order so ON so.order_no_ = spo.order_no_
		<include refid="queryStudentPaymentOrdersSql"/>
	</select>
</mapper>
