package com.fs.company.mapper;

import java.math.BigDecimal;
import java.util.List;
import com.fs.company.domain.CompanyMoneyLogs;
import com.fs.company.param.CompanyMoneyLogsParam;
import com.fs.company.param.CompanyStoreOrderMoneyLogsListParam;
import com.fs.company.vo.*;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

/**
 * 企业账户记录Mapper接口
 *
 * @author fs
 * @date 2021-10-04
 */
public interface CompanyMoneyLogsMapper
{
    /**
     * 查询企业账户记录
     *
     * @param logsId 企业账户记录ID
     * @return 企业账户记录
     */
    public CompanyMoneyLogs selectCompanyMoneyLogsById(Long logsId);

    /**
     * 查询企业账户记录列表
     *
     * @param companyMoneyLogs 企业账户记录
     * @return 企业账户记录集合
     */
    public List<CompanyMoneyLogs> selectCompanyMoneyLogsList(CompanyMoneyLogs companyMoneyLogs);

    /**
     * 新增企业账户记录
     *
     * @param companyMoneyLogs 企业账户记录
     * @return 结果
     */
    public int insertCompanyMoneyLogs(CompanyMoneyLogs companyMoneyLogs);

    /**
     * 修改企业账户记录
     *
     * @param companyMoneyLogs 企业账户记录
     * @return 结果
     */
    public int updateCompanyMoneyLogs(CompanyMoneyLogs companyMoneyLogs);

    /**
     * 删除企业账户记录
     *
     * @param logsId 企业账户记录ID
     * @return 结果
     */
    public int deleteCompanyMoneyLogsById(Long logsId);

    /**
     * 批量删除企业账户记录
     *
     * @param logsIds 需要删除的数据ID
     * @return 结果
     */
    public int deleteCompanyMoneyLogsByIds(Long[] logsIds);

    @Select({"<script> " +
            "select l.*,c.company_name " +
            "from company_money_logs l left join company c on c.company_id=l.company_id   " +
            "where 1=1 " +
            "<if test = 'maps.companyId != null  '> " +
            "and l.company_id = #{maps.companyId}" +
            "</if>" +
            "<if test = 'maps.logsType != null  '> " +
            "and l.logs_type = #{maps.logsType}" +
            "</if>" +
            "<if test = 'maps.businessId != null  '> " +
            "and l.business_id = #{maps.businessId}" +
            "</if>" +
            "<if test = 'maps.beginTime != null and maps.beginTime != \"\" '> " +
            "and date_format(l.create_time,'%y%m%d') &gt;= date_format(#{maps.beginTime},'%y%m%d') " +
            "</if>" +
            "<if test = 'maps.endTime != null and maps.endTime != \"\" '> " +
            "and date_format(l.create_time,'%y%m%d') &lt;= date_format(#{maps.endTime},'%y%m%d') " +
            "</if>" +
            "order by l.logs_id desc " +
            "</script>"})

    List<CompanyMoneyLogsVO> selectCompanyMoneyLogsVOList(@Param("maps") CompanyMoneyLogsParam companyMoneyLogs);


    @Select({"<script> " +
            "select l.*,c.company_name ,o.order_code,o.user_phone,p.bank_transaction_id,p.pay_type_code,p.pay_code " +
            "from company_money_logs l left join company c on c.company_id=l.company_id   " +
            "left join fs_store_order o on o.id=l.business_id " +
            "left join fs_store_payment p on o.id=p.order_id "+
            "where  (l.logs_type=4 || l.logs_type=5)  " +
            "<if test = 'maps.companyId != null  '> " +
            "and l.company_id = #{maps.companyId}" +
            "</if>" +
            "<if test = 'maps.logsType != null  '> " +
            "and l.logs_type = #{maps.logsType}" +
            "</if>" +
            "<if test = 'maps.businessId != null  '> " +
            "and l.business_id = #{maps.businessId}" +
            "</if>" +
            "<if test = 'maps.orderCode != null and maps.orderCode != \"\"  '> " +
            "and o.order_code = #{maps.orderCode}" +
            "</if>" +
            "<if test = 'maps.tradeCode != null and maps.tradeCode != \"\"  '> " +
            "and p.bank_transaction_id = #{maps.tradeCode}" +
            "</if>" +
            "<if test = 'maps.payCode != null and maps.payCode != \"\"  '> " +
            "and p.pay_code = #{maps.payCode}" +
            "</if>" +
            "<if test = 'maps.userPhone != null and maps.userPhone != \"\"  '> " +
            "and o.user_phone = #{maps.userPhone}" +
            "</if>" +
            "<if test = 'maps.beginTime != null and maps.beginTime != \"\" '> " +
            "and date_format(l.create_time,'%y%m%d') &gt;= date_format(#{maps.beginTime},'%y%m%d') " +
            "</if>" +
            "<if test = 'maps.endTime != null and maps.endTime != \"\" '> " +
            "and date_format(l.create_time,'%y%m%d') &lt;= date_format(#{maps.endTime},'%y%m%d') " +
            "</if>" +
            "order by l.logs_id desc " +
            "</script>"})
    List<CompanyMoneyLogsVO> selectCompanyMoneyLogsMallVOList(@Param("maps") CompanyMoneyLogsParam companyMoneyLogs);

    @Select({"<script> " +
            "select l.*,c.company_name ,p.bank_transaction_id,p.pay_type_code " +
            "from company_money_logs l left join company c on c.company_id=l.company_id   " +
            "left join fs_store_payment p on p.payment_id=l.business_id "+
            "where  (l.logs_type=8 || l.logs_type=9)  " +
            "<if test = 'maps.companyId != null  '> " +
            "and l.company_id = #{maps.companyId}" +
            "</if>" +
            "<if test = 'maps.logsType != null  '> " +
            "and l.logs_type = #{maps.logsType}" +
            "</if>" +
            "<if test = 'maps.businessId != null  '> " +
            "and l.business_id = #{maps.businessId}" +
            "</if>" +

            "<if test = 'maps.payCode != null and maps.payCode != \"\"  '> " +
            "and p.pay_code = #{maps.payCode}" +
            "</if>" +
            "<if test = 'maps.tradeCode != null and maps.tradeCode != \"\"  '> " +
            "and p.bank_transaction_id = #{maps.tradeCode}" +
            "</if>" +

            "<if test = 'maps.beginTime != null and maps.beginTime != \"\" '> " +
            "and date_format(l.create_time,'%y%m%d') &gt;= date_format(#{maps.beginTime},'%y%m%d') " +
            "</if>" +
            "<if test = 'maps.endTime != null and maps.endTime != \"\" '> " +
            "and date_format(l.create_time,'%y%m%d') &lt;= date_format(#{maps.endTime},'%y%m%d') " +
            "</if>" +
            "order by l.logs_id desc  " +
            "</script>"})
    List<CompanyMoneyLogsVO> selectCompanyMoneyLogsMallVO1List(@Param("maps") CompanyMoneyLogsParam companyMoneyLogs);




    @Select({"<script> " +
            "select ifnull(sum(l.money),0) " +
            "from company_money_logs l left join company c on c.company_id=l.company_id " +
            "where 1=1 " +
            "<if test = 'maps.companyId != null  '> " +
            "and l.company_id = #{maps.companyId}" +
            "</if>" +
            "<if test = 'maps.logsType != null  '> " +
            "and l.logs_type = #{maps.logsType}" +
            "</if>" +
            "<if test = 'maps.beginTime != null and maps.beginTime != \"\" '> " +
            "and date_format(l.create_time,'%y%m%d') &gt;= date_format(#{maps.beginTime},'%y%m%d') " +
            "</if>" +
            "<if test = 'maps.endTime != null and maps.endTime != \"\" '> " +
            "and date_format(l.create_time,'%y%m%d') &lt;= date_format(#{maps.endTime},'%y%m%d') " +
            "</if>" +
            "</script>"})
    BigDecimal selectCompanyTotalMoneys(@Param("maps")CompanyMoneyLogsParam companyMoneyLogs);

    @Select("select * from company_money_logs where business_id=#{id} and logs_type=#{type} limit 1  ")
    CompanyMoneyLogs selectCompanyMoneyLogsByOrderId(@Param("id") Long id,@Param("type")Integer type);

    @Delete("delete from company_money_logs where company_id=#{companyId}")
    int deleteCompanyMoneyLogsByCompanyId(Long companyId);

    @Select({"<script> " +
            "select l.*,c.company_name,o.order_code,o.delivery_id as deliveryId,p.pay_code,p.pay_type_code " +
            "from company_money_logs l left join company c on c.company_id=l.company_id left join fs_store_order o on (o.id=l.business_id and (l.logs_type=4 or  l.logs_type=5))   left join fs_store_payment p on (p.payment_id=l.business_id and (l.logs_type=8 or  l.logs_type=9))  " +
            "where 1=1 " +
            "<if test = 'maps.companyId != null  '> " +
            "and l.company_id = #{maps.companyId}" +
            "</if>" +
            "<if test = 'maps.logsType != null  '> " +
            "and l.logs_type = #{maps.logsType}" +
            "</if>" +
            "<if test = 'maps.beginTime != null and maps.beginTime != \"\" '> " +
            "and date_format(l.create_time,'%y%m%d') &gt;= date_format(#{maps.beginTime},'%y%m%d') " +
            "</if>" +
            "<if test = 'maps.endTime != null and maps.endTime != \"\" '> " +
            "and date_format(l.create_time,'%y%m%d') &lt;= date_format(#{maps.endTime},'%y%m%d') " +
            "</if>" +
            "order by l.logs_id desc " +
            "</script>"})
    List<CompanyMoneyLogsExportVO> selectCompanyMoneyLogsExportVOList(@Param("maps")CompanyMoneyLogs companyMoneyLogs);
    @Select({"<script> " +
            "select DISTINCT l.*,c.company_name,p.pay_type_code,o.order_code,o.real_name,o.user_phone,o.user_address,o.total_price,o.pay_money,o.pay_delivery,o.status,o.delivery_pay_status, " +
            "o.delivery_time,o.delivery_pay_time,o.delivery_id " +
            ",(select group_concat(dd.dept_name separator  ',') from company_dept dd where o.dept_id=dd.dept_id or find_in_set(dd.dept_id,d.ancestors)) as dept_name" +
            " from company_money_logs l left join company c on c.company_id=l.company_id left join fs_store_order o on o.id=l.business_id " +
            " left join (select * from fs_store_payment where status != 0) p on o.id=p.order_id left join company_dept d on d.dept_id=o.dept_id "+
            "where (l.logs_type=4||l.logs_type=5) " +
            "<if test = 'maps.companyId != null  '> " +
            "and l.company_id = #{maps.companyId} " +
            "</if>" +
            "<if test = 'maps.beginTime != null and maps.beginTime != \"\" '> " +
            "and date_format(l.create_time,'%y%m%d') &gt;= date_format(#{maps.beginTime},'%y%m%d') " +
            "</if>" +
            "<if test = 'maps.endTime != null and maps.endTime != \"\" '> " +
            "and date_format(l.create_time,'%y%m%d') &lt;= date_format(#{maps.endTime},'%y%m%d') " +
            "</if>" +
            "order by l.logs_id desc LIMIT 30000" +
            "</script>"})
    List<CompanyMoneyLogsExport1VO> selectCompanyMoneyLogsExport1VOList(@Param("maps")CompanyMoneyLogs companyMoneyLogs);

    @Select({"<script> " +
            "select l.*,c.company_name,p.pay_code,p.pay_type_code,p.bank_transaction_id  " +
            ",(select group_concat(dd.dept_name separator  ',') from company_dept dd where p.dept_id=dd.dept_id or find_in_set(dd.dept_id,d.ancestors)) as dept_name"+
            " from company_money_logs l left join company c on c.company_id=l.company_id left join fs_store_payment p on p.payment_id=l.business_id " +
            " left join company_dept d on d.dept_id=p.dept_id "+
            "where (l.logs_type=8||l.logs_type=9) " +
            "<if test = 'maps.companyId != null  '> " +
            "and l.company_id = #{maps.companyId}" +
            "</if>" +
            "<if test = 'maps.beginTime != null and maps.beginTime != \"\" '> " +
            "and date_format(l.create_time,'%y%m%d') &gt;= date_format(#{maps.beginTime},'%y%m%d') " +
            "</if>" +
            "<if test = 'maps.endTime != null and maps.endTime != \"\" '> " +
            "and date_format(l.create_time,'%y%m%d') &lt;= date_format(#{maps.endTime},'%y%m%d') " +
            "</if>" +
            "order by l.logs_id desc " +
            "</script>"})
    List<CompanyMoneyLogsExport2VO> selectCompanyMoneyLogsExport2VOList(@Param("maps")CompanyMoneyLogs companyMoneyLogs);

    @Select("select l.* from company_money_logs l where l.company_id=#{companyId} and logs_id >#{minId} order by l.logs_id asc " )
    List<CompanyMoneyLogs> selectCompanyMoneyLogsByCompanyId(@Param("companyId") Long companyId,@Param("minId")  String minId);

    @Select({"<script> " +
            "select l.*,c.company_name " +
            "from company_money_logs l left join company c on c.company_id=l.company_id   " +
            "where (l.logs_type=4 or l.logs_type=5) " +
            "<if test = 'maps.companyId != null  '> " +
            "and l.company_id = #{maps.companyId}" +
            "</if>" +
            "<if test = 'maps.businessId != null  '> " +
            "and l.business_id = #{maps.businessId}" +
            "</if>" +
            "order by l.logs_id desc " +
            "</script>"})
    List<CompanyStoreOrderMoneyLogsVO> selectCompanyStoreOrderMoneyLogsList(@Param("maps")CompanyStoreOrderMoneyLogsListParam moneyLogsMap);
}
