package cn.ysk.cashier.mybatis.mapper;

import cn.ysk.cashier.dto.shop.TbShopRechargeListDto;
import cn.ysk.cashier.dto.shop.TbShopRechargeRespDto;
import cn.ysk.cashier.dto.shop.TbShopUserQueryCriteria;
import cn.ysk.cashier.pojo.shop.TbShopUser;
import cn.ysk.cashier.vo.ShopUserInfoVo;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.ibatis.annotations.Select;

public interface ShopUserMapper extends BaseMapper<TbShopUser> {

    @Select("<script>" +
            "SELECT su.id as id, su.user_id as user_id, su.head_img as headImg, su.name as nickName, su.sex as sex, " +
            "su.amount as amount, 0 as totalScore, su.telephone as telephone, u.last_log_in_at as lastLoginAt, " +
            "su.birth_day as birthDay, su.is_vip as isVip, su.created_at as createAt " +
            "FROM tb_shop_user su " +
            "left join tb_user_info u on u.id = su.user_id " +
            "<where> " +
            "<if test='param.shopId != null and param.shopId != &quot;&quot; and param.shopId != 1'>" +
            "AND su.shop_id = #{param.shopId} " +
            "</if>" +
            "<if test='param.name != null and param.name != &quot;&quot;'>" +
            "AND (su.name like concat('%', #{param.name}, '%') or su.name LIKE concat( '%', #{param.name}, '%' ) " +
            " or su.telephone like concat('%', #{param.name}, '%') or su.telephone like concat('%', #{param.name}, '%'))" +
            "</if>" +
            "<if test='isVip != null'>" +
            "AND su.is_vip=#{isVip} " +
            "</if>" +
            "</where>" +
            "order by su.created_at" +
            "</script>")
    IPage<ShopUserInfoVo> queryUser(TbShopUserQueryCriteria param, Integer isVip, Page pageInfo);

    @Select("<script> " +
            "select " +
            "tsuf.id as id, " +
            "tsu.shop_id as shop_id, " +
            "tsi.shop_name, " +
            "tsu.id as user_id, " +
            "tsu.telephone as user_phone, " +
            "tsu.`name` as user_name, " +
            "tsuf.amount as recharge_amount, " +
            "tsuf.biz_name as recharge_type, " +
            "tsuf.create_time as recharge_time " +
            "            from tb_shop_user_flow  tsuf " +
            "            left join tb_shop_user as tsu  on  tsuf.shop_user_id = tsu.id " +
            "left join tb_shop_info as tsi on tsi.id = tsu.shop_id " +
            "where tsuf.create_time BETWEEN #{param.startTime} and #{param.endTime} and tsuf.biz_code in ('cashMemberIn', 'scanMemberIn', 'scanMemberAwardIn') " +
            "            and tsu.shop_id = #{param.shopId} " +
            "order by tsuf.create_time desc " +
            "</script>")
    IPage<TbShopRechargeRespDto> queryRechargeList(TbShopRechargeListDto param, Page pageInfo);
}

