package com.fs.company.mapper;

import java.math.BigDecimal;
import java.util.List;
import com.fs.company.domain.Company;
import com.fs.company.param.CompanyParam;
import com.fs.company.vo.CompanyCrmVO;
import com.fs.company.vo.CompanyNameVO;
import com.fs.company.vo.CompanyVO;
import com.fs.his.vo.OptionsVO;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

/**
 * 企业Mapper接口
 *
 * @author fs
 * @date 2021-10-04
 */
public interface CompanyMapper
{
    /**
     * 查询企业
     *
     * @param companyId 企业ID
     * @return 企业
     */
    public Company selectCompanyById(Long companyId);

    /**
     * 查询企业列表
     *
     * @param company 企业
     * @return 企业集合
     */
    public List<Company> selectCompanyList(Company company);

    /**
     * 新增企业
     *
     * @param company 企业
     * @return 结果
     */
    public int insertCompany(Company company);

    /**
     * 修改企业
     *
     * @param company 企业
     * @return 结果
     */
    public int updateCompany(Company company);

    /**
     * 删除企业
     *
     * @param companyId 企业ID
     * @return 结果
     */
    public int deleteCompanyById(Long companyId);

    /**
     * 批量删除企业
     *
     * @param companyIds 需要删除的数据ID
     * @return 结果
     */
    public int deleteCompanyByIds(Long[] companyIds);
    @Select({"<script> " +
            "select c.*,a.api_name as voice_api_name,u.user_name " +
            "from company c left join company_voice_api a on a.api_id=c.voice_api_id left join company_user u on u.user_id=c.user_id " +
            "where is_del=0 " +
            "<if test = 'maps.companyId != null  '> " +
            "and c.company_id = #{maps.companyId}" +
            "</if>" +
            "<if test = 'maps.companyName != null and  maps.companyName !=\"\"     '> " +
            "and c.company_name like CONCAT('%',#{maps.companyName},'%') " +
            "</if>" +
            "<if test = 'maps.status != null   '> " +
            "and c.status = #{maps.status}" +
            "</if>" +
            "order by c.company_id desc " +
            "</script>"})
    List<CompanyVO> selectCompanyVOList(@Param("maps")CompanyParam param);

    @Select({"<script> " +
            "select c.* " +
            "from company c " +
            "where is_del=0 " +
            "<if test = 'maps.companyId != null  '> " +
            "and c.company_id = #{maps.companyId}" +
            "</if>" +
            "<if test = 'maps.companyName != null and  maps.companyName !=\"\"     '> " +
            "and c.company_name like CONCAT('%',#{maps.companyName},'%') " +
            "</if>" +
            "<if test = 'maps.status != null   '> " +
            "and c.status = #{maps.status}" +
            "</if>" +
            "order by c.company_id asc " +
            "</script>"})
    List<CompanyNameVO> selectCompanyNameVOList(@Param("maps")CompanyParam param);

    @Select({"<script> " +
            "select c.*,a.api_name as voice_api_name,u.user_name " +
            " ,(select count(1) from crm_customer cc where c.company_id= cc.company_id and date_format(cc.update_time,'%y%m%d') = date_format(now(),'%y%m%d') ) as now_day_customer_count " +
            "from company c left join company_voice_api a on a.api_id=c.voice_api_id left join company_user u on u.user_id=c.user_id " +
            "where is_del=0 " +
            "<if test = 'maps.companyId != null  '> " +
            "and c.company_id = #{maps.companyId}" +
            "</if>" +
            "<if test = 'maps.companyName != null and  maps.companyName !=\"\"     '> " +
            "and c.company_name like CONCAT('%',#{maps.companyName},'%') " +
            "</if>" +

            "<if test = 'maps.phonenumber != null and  maps.phonenumber !=\"\"     '> " +
            "and c.company_mobile like CONCAT('%',#{maps.phonenumber},'%') " +
            "</if>" +

            "<if test = 'maps.status != null   '> " +
            "and c.status = #{maps.status}" +
            "</if>" +
            "</script>"})
    List<CompanyCrmVO> selectCompanyCrmDayCountList(@Param("maps")CompanyParam param);

    @Select("select count(1) from company")
    Integer selectCompanyCount();
    @Select("select * from company where app_id=#{appId} limit 1")
    Company selectCompanyByAppId(String appId);
    @Select("update company set money=money+#{tuiMoney} where company_id=#{companyId} ")
    int updateCompanyMoney(@Param("companyId")Long companyId, @Param("tuiMoney") BigDecimal tuiMoney);
    @Select("select * from company where company_id=#{companyId} for update")
    Company selectCompanyByIdForUpdate(Long companyId);

    @Select("select oms_code as omsCode from company where company_id=#{companyId}")
    String selectOmsCodeByCompanyId(Long companyId);

    @Update("update company set oms_code =#{omsCode} where company_id =#{companyId} ")
    void updateOmsCodeByCompanyIds(@Param("companyId") Long companyId,@Param("omsCode") String omsCode);

    @Select("select company_id,company_name from company where oms_code = #{omsCode}")
    List<Long> selectCompanyByOmsCode(@Param("omsCode") String omsCode);


    @Select("select company_id dictValue,company_name dictLabel from company")
    List<OptionsVO> selectAllCompanyList();

    void updateMoney(@Param("id") Long id, @Param("money") BigDecimal money);
}
