package com.fs.company.mapper;

import java.util.List;
import com.fs.company.domain.CompanySms;
import com.fs.company.vo.CompanySmsListVO;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

/**
 * 公司短信Mapper接口
 *
 * @author fs
 * @date 2023-01-09
 */
public interface CompanySmsMapper {
    /**
     * 查询公司短信
     *
     * @param smsId 公司短信ID
     * @return 公司短信
     */
    public CompanySms selectCompanySmsById(Long smsId);


    /**
     * 新增公司短信
     *
     * @param companySms 公司短信
     * @return 结果
     */
    public int insertCompanySms(CompanySms companySms);

    /**
     * 修改公司短信
     *
     * @param companySms 公司短信
     * @return 结果
     */
    public int updateCompanySms(CompanySms companySms);

    /**
     * 删除公司短信
     *
     * @param smsId 公司短信ID
     * @return 结果
     */
    public int deleteCompanySmsById(Long smsId);

    /**
     * 批量删除公司短信
     *
     * @param smsIds 需要删除的数据ID
     * @return 结果
     */
    public int deleteCompanySmsByIds(Long[] smsIds);

    @Select("select * from company_sms where company_id=#{companyId}")
    CompanySms selectCompanySmsByCompanyId(Long companyId);

    @Select({"<script> " +
            "select s.*,c.company_name from company_sms  s left join company c on c.company_id=s.company_id " +
            "where 1=1 " +
            "<if test = 'maps.companyId != null  '> " +
            "and s.company_id = #{maps.companyId}" +
            "</if>" +
            "order by s.sms_id desc " +
            "</script>"})
    public List<CompanySmsListVO> selectCompanySmsList(@Param("maps") CompanySms companySms);

    @Update("update company_sms set remain_sms_count=remain_sms_count-#{number} where company_id=#{companyId}")
    int subCompanySms(@Param("companyId") Long companyId, @Param("number") int number);

    @Select("select * from company_sms where company_id=#{companyId} for update")
    CompanySms selectCompanySmsByCompanyIdForUpdate(Long companyId);

    @Update("update company_sms set remain_sms_count=remain_sms_count+#{number} where company_id=#{companyId}")
    int addCompanySms(@Param("companyId") Long companyId, @Param("number") int number);

    @Select("select SUM(number) from company_sms_logs where status = 1 and company_id=#{companyId}")
    Long successSmsCount(Long companyId);

    @Select("select SUM(number) from company_sms_logs where status = -1 and company_id=#{companyId}")
    Long failSmsCount(Long companyId);

    @Select("select SUM(number) from company_sms_logs where status is not null and company_id=#{companyId}")
    Long sendSmsCount(Long companyId);

    @Select("select SUM(number) from company_sms_logs where status != -1 and company_id=#{companyId}")
    Long debitSmsCount(Long companyId);


}
