package com.fs.qw.mapper;

import com.alibaba.fastjson.JSONObject;
import com.fs.qw.domain.QwContactWay;
import com.fs.qw.param.QwStatisticsParam;
import com.fs.qw.vo.QwWayStatisticsListVO;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;

/**
 * 企微活码Mapper接口
 *
 * @author fs
 * @date 2024-07-05
 */
@SuppressWarnings("ALL")
@Repository
public interface QwContactWayMapper
{
    /**
     * 查询企微活码
     *
     * @param id 企微活码主键
     * @return 企微活码
     */
    public QwContactWay selectQwContactWayById(Long id);

    /**
     * 查询企微活码列表
     *
     * @param qwContactWay 企微活码
     * @return 企微活码集合
     */
    public List<QwContactWay> selectQwContactWayList(QwContactWay qwContactWay);

    /**
     * 新增企微活码
     *
     * @param qwContactWay 企微活码
     * @return 结果
     */
    public int insertQwContactWay(QwContactWay qwContactWay);

    /**
     * 修改企微活码
     *
     * @param qwContactWay 企微活码
     * @return 结果
     */
    public int updateQwContactWay(QwContactWay qwContactWay);

    /**
     * 删除企微活码
     *
     * @param id 企微活码主键
     * @return 结果
     */
    public int deleteQwContactWayById(Long id);

    /**
     * 批量删除企微活码
     *
     * @param ids 需要删除的数据主键集合
     * @return 结果
     */
    public int deleteQwContactWayByIds(Long[] ids);
    @Select("select * from qw_contact_way where config_id=#{configId} and corp_id=#{corpId} limit 1")
    QwContactWay selectQwContactWayByConfigIdAndCompanyId(@Param("configId") String configId,@Param("corpId") String corpId);



    @Select("select * from qw_contact_way where user_type=2")
    List<QwContactWay> selectQwContactWayByUserTime();

    @Select({"<script> " +
            "select u.nick_name,ROUND(AVG(DATEDIFF(o.create_time, cu.create_time)))  AS customerCycle,count(1) customerOkNum from fs_store_order o left join crm_customer cu ON o.customer_id = cu.customer_id LEFT JOIN company_user u on u.user_id=o.company_user_id  WHERE is_first=1 " +
            " " +
            "<if test = 'maps.type != null and maps.type ==1 '> " +
            "and TO_DAYS(cu.create_time) = TO_DAYS(NOW()) " +
            "</if>" +
            "<if test = 'maps.type != null and maps.type ==2 '> " +
            "and TO_DAYS(NOW()) - TO_DAYS(cu.create_time) &lt;= 1 " +
            "</if>" +
            "<if test = 'maps.type != null and maps.type ==3 '> " +
            "and YEARWEEK(DATE_FORMAT(cu.create_time,'%Y-%m-%d')) = YEARWEEK(NOW()) " +
            "</if>" +
            "<if test = 'maps.type != null and maps.type ==4 '> " +
            "and YEARWEEK(DATE_FORMAT(cu.create_time,'%Y-%m-%d')) = YEARWEEK(NOW())-1 " +
            "</if>" +
            "<if test = 'maps.type != null and maps.type ==5 '> " +
            "and DATE_FORMAT(cu.create_time,'%Y%m') = DATE_FORMAT(CURDATE(),'%Y%m') " +
            "</if>" +
            "<if test = 'maps.type != null and maps.type ==6 '> " +
            "and PERIOD_DIFF(DATE_FORMAT(NOW(),'%Y%m'),DATE_FORMAT(cu.create_time,'%Y%m')) = 1 " +
            "</if>" +
            "<if test = 'maps.type != null and maps.type ==7 '> " +
            "and QUARTER(cu.create_time) = QUARTER(NOW()) " +
            "</if>" +
            "<if test = 'maps.type != null and maps.type ==8 '> " +
            "and QUARTER(cu.create_time) = QUARTER(DATE_SUB(NOW(),INTERVAL 1 QUARTER)) " +
            "</if>" +
            "<if test = 'maps.type != null and maps.type ==9 '> " +
            "and YEAR(cu.create_time)=YEAR(NOW()) " +
            "</if>" +
            "<if test = 'maps.type != null and maps.type ==10 '> " +
            "and  YEAR(cu.create_time) = YEAR(DATE_SUB(NOW(),INTERVAL 1 YEAR))" +
            "</if>" +
            "<if test = 'maps.startTime != null and maps.startTime != \"\" '> " +
            "AND date_format(cu.create_time,'%y%m%d') &gt;= date_format(#{maps.startTime},'%y%m%d')"+
            "</if>" +
            "<if test = 'maps.endTime != null and maps.endTime != \"\" '> " +
            "AND date_format(cu.create_time,'%y%m%d') &lt;= date_format(#{maps.endTime},'%y%m%d')"+
            "</if>" +
            "and  cu.create_user_id IN " +
            "<foreach  item='item' index='index' collection='maps.users' open='(' separator=',' close=')'> #{item}    </foreach>"+
            " group by o.company_user_id "+
            "</script>"})
    List<QwWayStatisticsListVO> QwWayStatisticsListVO(@Param("maps")QwStatisticsParam param);

    List<JSONObject> selectQwWayStatisticsCounts(Map<String, Object> map);
    @Update("UPDATE qw_contact_way SET add_num = add_num + 1,num=num + 1 where id=#{id};")
    void addContactWayNum(Long id);
    @Update("UPDATE qw_information SET add_num = add_num + 1,num=num + 1 where id=#{informationId};")
    void addInformationNum(Long informationId);
}
