<?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="co.yixiang.yshop.module.product.dal.mysql.teamworkinfo.TeamworkInfoMapper">

    <!--
        一般情况下，尽可能使用 Mapper 进行 CRUD 增删改查即可。
        无法满足的场景，例如说多表关联查询，才使用 XML 编写 SQL。
        代码生成器暂时只生成 Mapper XML 文件本身，更多推荐 MybatisX 快速开发插件来生成查询。
        文档可见：https://www.yixiang.co/MyBatis/x-plugins/
     -->

    <select id="getTeamworkInfoPage"
            resultType="co.yixiang.yshop.module.product.controller.admin.teamworkinfo.vo.TeamworkInfoRespVO">
        select yti.id, yti.campaign_id, yti.people as joinNumber, yci.person, yti.state,
        yu.nickname as regimentalCommander, yti.create_time,
        if(yspav.image is null or yspav.image ='',ysp.image,yspav.image) as image,
        ysp.store_name as productName,yspav.sku,yci.name
        from yshop_teamwork_info as yti
        inner join yshop_campaign_info as yci on yti.campaign_id = yci.id
        inner join yshop_campaign_detail as ycd on yti.campaign_detail_id = ycd.id
        inner join yshop_store_product_attr_value as yspav on ycd.sku_id = yspav.id
        inner join yshop_store_product as ysp on ycd.product_id = ysp.id
        inner join yshop_user as yu on yu.id = yti.regimental_commander_id
        where 1 = 1
        <if test="pageReqVO.campaignName != null and pageReqVO.campaignName != ''">
            and yci.`name` LIKE CONCAT('%',#{pageReqVO.campaignName},'%')
        </if>
        <if test="pageReqVO.state != null">
            and yti.`state` = #{pageReqVO.state}
        </if>
        <if test="pageReqVO.startTime != null and pageReqVO.startTime != '' and pageReqVO.endTime != null and pageReqVO.endTime != ''">
            and yti.create_time between #{pageReqVO.startTime} and #{pageReqVO.endTime}
        </if>
        order by yti.id desc
    </select>

    <select id="getTeamworkOrderInfoPage"
            resultType="co.yixiang.yshop.module.product.controller.admin.teamworkinfo.vo.TeamworkOrderInfoRespVO">
        select yso.pay_price as price, yso.order_id, yu.nickname,yu.avatar
        from yshop_store_order as yso
        inner join yshop_user as yu on yu.id = yso.uid
        where yso.teamwork_id = #{pageReqVO.id}
        and yso.paid = 1 and yso.refund_status in (0,1)
        order by yso.id
    </select>

    <select id="getNotTeamwork" resultType="java.lang.Long">
        select yso.id from yshop_teamwork_info as yti
        inner join yshop_store_order as yso on yti.id = yso.teamwork_id
        where yso.paid = 1 and yso.refund_status in (0,1) and yti.state = 1
        and yti.campaign_id = #{campaignId}
    </select>

    <update id="teamworkFail">
        update yshop_teamwork_info set state = 2
        where state = 0 and campaign_id = #{campaignId}
    </update>

    <select id="getWaitTeamworkList"
            resultType="co.yixiang.yshop.module.product.controller.app.teamworkinfo.vo.AppWaitTeamworkRespVO">
        select yti.id, yti.people as `current`, yti.create_time, yci.effective_time, yci.person as total, ycd.sku_id
        from yshop_teamwork_info as yti
        inner join yshop_campaign_info as yci on yci.id = yti.campaign_id
        inner join yshop_campaign_detail as ycd on ycd.id = yti.campaign_detail_id
        where yti.state = 0
        <if test="vo.skuId != null ">
            and ycd.sku_id = #{vo.skuId}
        </if>
        <if test="vo.campaignDetailId != null ">
            and yti.campaign_detail_id = #{campaignDetailId}
        </if>
        <if test="vo.campaignId != null ">
            and yti.campaign_id = #{campaignId}
        </if>
    </select>

    <select id="getWaitTeamworkHeadList"
            resultType="co.yixiang.yshop.module.product.controller.app.teamworkinfo.vo.AppWaitTeamworkUserRespVO">
        select yti.id, yso.uid, yso.order_id, yu.nickname,yu.avatar, 1 as isHead
        from yshop_teamwork_info as yti
        inner join yshop_store_order as yso on yti.id = yso.teamwork_id
        inner join yshop_user as yu on yu.id = yso.uid
        where yso.teamwork_id in (
        <foreach collection="ids" item="id" separator=",">
            #{id}
        </foreach>
        )
        and yti.regimental_commander_id = yso.uid and yso.paid = 1 and yso.refund_status in (0,1)
    </select>

    <select id="getWaitTeamworkUserList"
            resultType="co.yixiang.yshop.module.product.controller.app.teamworkinfo.vo.AppWaitTeamworkUserRespVO">
        select yti.id, yso.uid, yso.order_id, yu.nickname,yu.avatar,
        if(yti.regimental_commander_id = yso.uid, 1, 0) as isHead
        from yshop_teamwork_info as yti
        inner join yshop_store_order as yso on yti.id = yso.teamwork_id
        inner join yshop_user as yu on yu.id = yso.uid
        where yso.teamwork_id = #{id} and yso.paid = 1 and yso.refund_status in (0,1)
    </select>

    <select id="getProductDetail"
            resultType="co.yixiang.yshop.module.product.controller.app.teamworkinfo.vo.AppTeamworkProductDetailRespVO">
        select ysp.id, yspav.id as skuId, yspav.`unique` as skuUnique, yci.person,
        yti.state, ycd.price,
        date_add(yti.create_time, interval yci.effective_time minute) as closeTime,
        if(yspav.image is null or yspav.image ='',ysp.image,yspav.image) as image,
        ysp.store_name,yspav.sku
        from yshop_teamwork_info as yti
        inner join yshop_campaign_info as yci on yti.campaign_id = yci.id
        inner join yshop_campaign_detail as ycd on yti.campaign_detail_id = ycd.id
        inner join yshop_store_product_attr_value as yspav on ycd.sku_id = yspav.id
        inner join yshop_store_product as ysp on ycd.product_id = ysp.id
        where yti.id = #{id}
    </select>


    <select id="joinJudge" resultType="java.lang.Long">
        select id
        from yshop_store_order
        where uid = #{uid} and teamwork_id = #{teamworkId} and paid = 1 and refund_status in (0,1)
    </select>

    <select id="randomAvatar" resultType="java.lang.String">
        SELECT `avatar` FROM yshop_user where id not in (
        <foreach collection="userIds" item="userId" separator=",">
            #{userId}
        </foreach>
        )
        ORDER BY RAND() LIMIT #{number};
    </select>

    <select id="getMember" resultType="java.lang.Long">
        select yso.uid
        from yshop_store_order as yso
        inner join yshop_teamwork_info as yti on yti.oid = yso.id
        where paid = 1 and refund_status in (0,1) and yso.uid != #{userId} and yti.id = #{id}
    </select>

</mapper>
