package com.yunzhi.marketing.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yunzhi.marketing.entity.TaBuildingDynamic;
import com.yunzhi.marketing.entity.TaChannel;
import com.yunzhi.marketing.entity.TaPerson;
import com.yunzhi.marketing.entity.TaPersonPositon;
import com.yunzhi.marketing.po.PersonPO;
import org.apache.ibatis.annotations.*;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 人员表 Mapper 接口
 * </p>
 *
 * @author jobob
 * @since 2019-05-10
 */
@Mapper
public interface TaPersonMapper extends BaseMapper<TaPerson> {
    List<Map<String, Object>> selectCardList(IPage<Map<String, Object>> page, @Param("personType") String personType, @Param("status") int status, @Param("buildingId") String buildingId, @Param("orgId") Integer orgId, @Param("consultant") String consultant);

    int setFieldIncrement(@Param("personId") String personId, @Param("field") String field, @Param("increment") int increment);

    int setPointsIncrement(@Param("personId") String personId, @Param("increment") int increment);

    TaPerson getSalesExecutive();

    List<TaPerson> getPersonByOpenId(@Param("openId") String openId);

    List<Map<String, Object>> selectBuildingConsultants(@Param("buildingId") String buildingId, @Param("personType") String personType, @Param("status") int status, @Param("orgId") Integer orgId);

    IPage<TaPerson> selectCustomer(IPage<TaPerson> page);

    TaPerson getById(@Param("personId") String personId);


    List<String> getUserProjects(@Param("personId") Integer personId);

    TaPerson selectByIdPerson(@Param("openid") String openid);

    List<String> getConsultantByName(@Param("name") String name);

    String getConsultantByTel(@Param("tel") String tel);

    List<Map<String, Object>> selectCardListofMine(IPage<Map<String, Object>> page, @Param("personType") String personType, @Param("status") int status, @Param("personIds") List<String> personIds);

    //---------- 用户总数 start ------------

    /**
     * 总用户数
     *
     * @return
     */
    @ResultType(Integer.class)
    @Select("select count(1) from ta_person where org_id = #{org} AND person_type IN ('customer','drift','estate agent')")
    Integer selectUserCount(@Param("org") Integer orgId);

    /**
     * 总注册数
     *
     * @return
     */
    @ResultType(Integer.class)
    @Select("select count(1) from ta_person where org_id = #{org} AND person_type IN ('customer','drift','estate agent') and phone is not null")
    Integer selectRegisteredCount(@Param("org") Integer orgId);

    /**
     * 渠道代理后台总用户数
     *
     * @return
     */
    @ResultType(Integer.class)
    @Select("select count(1) from ta_person where org_id = #{org}")
    Integer selectChannelUserCount(@Param("org") String orgId);

    /**
     * 渠道代理后台总注册数
     *
     * @return
     */
    @ResultType(Integer.class)
    @Select("select count(1) from ta_person where org_id = #{org} and phone is not null")
    Integer selectChannelRegisteredCount(@Param("org") String orgId);

    /**
     * 根据时间段查询
     *
     * @param personType
     * @return
     */
    Integer selectRecentlyCount(@Param("org") Integer orgId, @Param("personType") String personType, @Param("startDate") LocalDate startDate, @Param("endDate") LocalDate endDate);

    //---------- 用户总数 end ------------

    /**
     * 用户行为
     *
     * @param personType
     * @param startDate
     * @param endDate
     * @return
     */
    List<PersonPO> selectUserBehavior(@Param("org") Integer orgId,
                                      @Param("personType") String personType,
                                      @Param("startDate") LocalDate startDate,
                                      @Param("endDate") LocalDate endDate,
                                      @Param("buildingId") String buildingId,
                                      @Param("eventType") String eventType,
                                      @Param("event") String event,
                                      @Param("activity") String activity);

    /**
     * 活跃用户数 / 最近新增的用户数
     *
     * @param personType
     * @param dateType
     * @return
     */
    List<Map<String, Object>> selectActiveUserCount(@Param("org") Integer orgId, @Param("personType") String personType, @Param("dateType") String dateType, @Param("startDate") LocalDate startDate, @Param("endDate") LocalDate endDate);

    /**
     * 新增用户数
     *
     * @param personType
     * @param startDate
     * @param endDate
     * @return
     */
    List<Map<String, Object>> selectNewsUserCount(@Param("orgId") Integer orgId, @Param("personType") String personType, @Param("startDate") LocalDate startDate, @Param("endDate") LocalDate endDate);

    /**
     * 性别比例
     * <p>
     * 据说 1 男  2 女
     *
     * @return
     */
    @Select("select count(1) as sex_count from ta_person where org_id = #{org} AND person_type IN ('customer','drift','estate agent')  and gender = #{gender}")
    Integer selectSexUser(@Param("org") Integer orgId, @Param("gender") Integer gender);


    /**
     * 城市比例
     *
     * @param personType
     * @return
     */
    @ResultType(Map.class)
    @Select("SELECT " +
            " 	a.city as city, " +
            "	b.shortname AS name, " +
            "	b.lng, " +
            "	b.lat, " +
            "	count(a.person_id) AS cityCount " +
            "FROM " +
            "	ta_person a " +
            "inner JOIN td_city b " +
            "on a.city = b.id " +
            "WHERE " +
            "   a.org_id = #{org} " +
            "	AND a.person_type IN ('customer','drift','estate agent')  " +
            "GROUP BY " +
            "	a.city ")
    List<Map<String, Object>> selectCityUser(@Param("org") Integer orgId);


    // ------------- 用户来源 start ------------

    /**
     * 用户来源 的饼图
     *
     * @param recommendPersonType
     * @return
     */
    Integer selectUserSourcePie(@Param("org") Integer orgId, @Param("recommendPersonType") String recommendPersonType, @Param("personType") String personType);

    /**
     * 用户来源 柱状
     *
     * @return
     */
    List<Map<String, Object>> selectUserSourceColumnar(@Param("org") Integer orgId, @Param("personType") String personType, @Param("startDate") LocalDate startDate, @Param("endDate") LocalDate endDate);

    /**
     * 用户来源 数据列表
     */
    List<Map<String, Object>> selectUserSourceData(@Param("org") Integer orgId, @Param("personType") String personType, @Param("startDate") LocalDate startDate, @Param("endDate") LocalDate endDate);

    // ------------- 用户来源 end ------------

    @Update("UPDATE ta_person  SET ${field} = IFNULL(${field}, 0) + #{increment}  WHERE person_id = #{newsId}")
    void setFieldNum(@Param("newsId") String newsId, @Param("field") String field, @Param("increment") int increment);

    IPage<TaPerson> getPersonList(IPage<TaPerson> page, @Param("personType") String personType, @Param("name") String name, @Param("phone") String phone, @Param("status") Integer status, @Param("buildingId") String buildingId, @Param("personTags") String personTags);

    /**
     * 渠道下的经纪人
     *
     * @param pg
     * @param name
     * @param phone
     * @return
     */
    IPage<TaPerson> channelBrokerList(IPage<TaChannel> pg, @Param("name") String name, @Param("phone") String phone, @Param("orgid") Integer orgid, @Param("channelId") Integer channelId);

    /**
     * 渠道邀请经纪人
     *
     * @param pg
     * @param id
     * @return
     */
    IPage<TaPerson> InviteClientsList(IPage<TaChannel> pg, @Param("id") String id);

    /**
     * 获取用户总积分
     *
     * @param orgId
     * @return
     */
    Integer sumPointsByOrgId(@Param("orgId") Integer orgId);

    //更新city拼音为cityId
    void upDatePersonCityToId(@Param("personId") String personId);

    /**
     * 判断置业顾问绑定状态
     *
     * @return
     */
    Integer judgeBindStatusByCondition(@Param("userId") Integer userId, @Param("orgId") Integer orgId, @Param("phone") String phone);

    IPage<TaPerson> selectDriftCustomerPageList(IPage<TaPerson> page, @Param("buildingId") String buildingId, @Param("orgId") Integer orgId, @Param("personEstateDrift") String personEstateDrift, @Param("sceneType") String sceneType);

    int selectFromNum(@Param("orgId") Integer orgId, @Param("personType") String personType);

    /**
     * 保存用户位置信息
     *
     * @param record
     * @return
     */
    Integer savePersonPosition(TaPersonPositon record);

    /**
     * 今日新增人数
     * @param orgId
     * @return
     */
    Integer selectTodayRecentlyCount(@Param("orgId") Integer orgId, @Param("nowDate") LocalDateTime nowDate);

    List<Map<String, Object>> selectCityPerson(@Param("orgId") Integer orgId);

    Integer cancelConsulatant(@Param("personId") String personId);

    IPage<TaPerson> getMarketingList(Page<TaBuildingDynamic> page,@Param("buildingId") String buildingId);
}
