package com.fs.company.mapper;

import java.math.BigDecimal;
import java.util.List;
import java.util.Set;

import com.fs.company.domain.Company;
import com.fs.company.domain.CompanyUser;
import com.fs.company.param.CompanyLiveShowParam;
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.deptId != null'> " +
            "and c.dept_id = #{maps.deptId} " +
            "</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.deptId != null'> " +
            "and c.dept_id = #{maps.deptId} " +
            "</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({"<script> " +
            "select c.*,cu.user_name,qu.used_num," +
            "MAX(CASE WHEN cm.type = 0 THEN sc.`name` END) AS masterAppName,"+
            "MAX(CASE WHEN cm.type = 1 THEN sc.`name` END) AS backupAppName,"+
            "CASE WHEN JSON_VALID(t1.config_value) THEN t1.config_value->>'$.mchId' ELSE NULL END as mchId" +
            " FROM company c LEFT JOIN company_user cu ON c.user_id =cu.user_id  " +
            " left join company_config t1 on t1.config_key = 'redPacket:config' and t1.company_id = c.company_id " +
            "LEFT JOIN (select company_id, count(id) as used_num from qw_user where server_id is not null and server_status = 1 group by company_id) qu ON qu.company_id = c.company_id " +
            "LEFT JOIN company_miniapp cm ON c.company_id = cm.company_id\n" +
            "LEFT JOIN fs_course_play_source_config sc ON sc.appid = cm.app_id "+
            "where c.is_del=0 " +
            "            <if test=\"companyName != null  and companyName != ''\"> and c.company_name like concat('%', #{companyName}, '%')</if>\n" +
            "            <if test=\"companyMobile != null  and companyMobile != ''\"> and c.company_mobile = #{companyMobile}</if>\n" +
            "            <if test=\"companyAddress != null  and companyAddress != ''\"> and c.company_address = #{companyAddress}</if>\n" +
            "            <if test=\"status != null \"> and c.status = #{status}</if>\n" +
            "            <if test=\"startTime != null \"> and c.start_time = #{startTime}</if>\n" +
            "            <if test=\"limitTime != null \"> and c.limit_time = #{limitTime}</if>\n" +
            "            <if test=\"money != null \"> and c.money = #{money}</if>\n" +
            "            <if test=\"companyType != null \"> and c.company_type = #{companyType}</if>\n" +
            "            <if test=\"userId != null \"> and c.user_id = #{userId}</if>\n" +
            "            <if test=\"linkName != null  and linkName != ''\"> and c.link_name like concat('%', #{linkName}, '%')</if>\n" +
            "            <if test=\"limitUserCount != null \"> and c.limit_user_count = #{limitUserCount}</if>\n" +
            "            <if test=\"isDel != null \"> and c.is_del = #{isDel}</if>\n" +
            "            <if test=\"deptId != null \"> and c.dept_id = #{deptId}</if> " +
            "            <if test=\"queryAppName != null and queryAppName != ''\">\n" +
            "                AND EXISTS (\n" +
            "                SELECT 1 FROM company_miniapp cm2\n" +
            "                LEFT JOIN fs_course_play_source_config sc2 ON sc2.appid = cm2.app_id\n" +
            "                WHERE cm2.company_id = c.company_id\n" +
            "                AND sc2.`name` LIKE CONCAT('%', #{queryAppName}, '%')\n" +
            "                )\n" +
            "            </if> "+
            " GROUP BY c.company_id " +
            " order by company_id desc" +
            "</script>"})
    List<CompanyVO> selectCompanyListVO(Company param);

    List<OptionsVO> selectAllCompanyList(@Param("deptId") Long deptId);

    @Select("select company_id from company")
    List<Long> selectCompanyIds();

    String selectCompanyNameCompanyByIds(@Param("companyIds") String companyIds);

    String selectDoctorIdsByCompanyId(Long companyId);

    List<Company> selectCompanyAllList();

    List<OptionsVO> selectByIds(@Param("ids") List<Long> ids);


    /**
     * 通过企业id批量查询
     * **/
    List<Company> selectCompanyByIds(@Param("ids") List<Long> ids);

    List<Company> selectCompanyByIds2(@Param("companyIds") Set<Long> companyIds);


    List<CompanyUser> selectCompanyListByIds(@Param("userIds") String result);

    /**
     * 查询企微主体管理公司列表
     */
    List<OptionsVO> getCompanyListByCorpId(@Param("corpId") String corpId);

    List<Company> selectCompanyMoneyAllList();

    /**
     * 批量修改公司余额
     * @param list
     */
    void batchUpdateCompany(@Param("list") List<Company> list);

    @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.liveShow != null  '> " +
            "and c.live_show = #{maps.liveShow}" +
            "</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> liveShowList(@Param("maps")CompanyParam param);

    @Update({"<script> " +
            "update company set live_show = #{maps.liveShow} where " +
            "company_id in " +
            "<foreach collection='maps.ids' item='ids' open='(' separator=',' close=')'>",
            " #{ids} ",
            "</foreach> </script>"
    })
    void batchUpdateLiveShow(@Param("maps") CompanyLiveShowParam param);

    @Select("select company_id from company where live_show=1")
    List<Long> selectLiveShowCompanyId();

    @Select("select company_id,company_name from company where \n" +
            " `status` != 0   " +
            " and is_del != 1 ")
    List<CompanyVO> getCompanyDropList();

    @Select("select company_id,company_name from company where FIND_IN_SET( company_id , (select company_ids from qw_company where corp_id = #{corpId})) ")
    List<Company> getCompanyList(@Param("corpId") String corpId);
}
