package com.fs.his.mapper;

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

import com.alibaba.fastjson.JSONObject;
import com.fs.company.param.FsStoreStatisticsParam;
import com.fs.company.vo.FsStorePaymentStatisticsVO;
import com.fs.his.domain.FsStorePayment;
import com.fs.his.param.FsStorePaymentParam;
import com.fs.his.vo.FsStorePaymentExcelVO;
import com.fs.his.vo.FsStorePaymentVO;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

/**
 * 支付明细Mapper接口
 *
 * @author fs
 * @date 2023-08-11
 */
public interface FsStorePaymentMapper
{
    /**
     * 查询支付明细
     *
     * @param paymentId 支付明细主键
     * @return 支付明细
     */
    public FsStorePayment selectFsStorePaymentByPaymentId(Long paymentId);

    /**
     * 查询支付明细列表
     *
     * @param fsStorePayment 支付明细
     * @return 支付明细集合
     */
    public List<FsStorePayment> selectFsStorePaymentList(FsStorePayment fsStorePayment);

    /**
     * 新增支付明细
     *
     * @param fsStorePayment 支付明细
     * @return 结果
     */
    public int insertFsStorePayment(FsStorePayment fsStorePayment);

    /**
     * 修改支付明细
     *
     * @param fsStorePayment 支付明细
     * @return 结果
     */
    public int updateFsStorePayment(FsStorePayment fsStorePayment);

    /**
     * 删除支付明细
     *
     * @param paymentId 支付明细主键
     * @return 结果
     */
    public int deleteFsStorePaymentByPaymentId(Long paymentId);

    /**
     * 批量删除支付明细
     *
     * @param paymentIds 需要删除的数据主键集合
     * @return 结果
     */
    public int deleteFsStorePaymentByPaymentIds(Long[] paymentIds);

    @Select({"<script> " +
            "SELECT sp.*,u.nick_name,u.phone,s.store_name,c.company_name,cu.nick_name as company_user_nick_name FROM fs_store_payment sp " +
            " LEFT JOIN  fs_user u ON u.user_id=sp.user_id LEFT JOIN fs_store s ON s.store_id=sp.store_id " +
            " left join company_user cu on sp.company_user_id=cu.user_id " +
            " left join company c on c.company_id=sp.company_id "+
            " where 1=1 " +
            "            <if test=\"maps.payCode != null  and maps.payCode != ''\"> and sp.pay_code = #{maps.payCode}</if>\n" +
            "            <if test=\"maps.payTypeCode != null  and maps.payTypeCode != ''\"> and sp.pay_type_code = #{maps.payTypeCode}</if>\n" +
            "            <if test=\"maps.businessCode != null  and maps.businessCode != ''\"> and sp.business_code = #{maps.businessCode}</if>\n" +
            "            <if test=\"maps.tradeNo != null  and maps.tradeNo != ''\"> and sp.trade_no = #{maps.tradeNo}</if>\n" +
            "            <if test=\"maps.userId != null \"> and sp.user_id = #{maps.userId}</if>\n" +
            "            <if test=\"maps.openId != null  and openId != ''\"> and sp.open_id = #{maps.openId}</if>\n" +
            "            <if test=\"maps.businessType != null \"> and sp.business_type = #{maps.businessType}</if>\n" +
            "            <if test=\"maps.businessId != null  and maps.businessId != ''\"> and sp.business_id = #{maps.businessId}</if>\n" +
            "            <if test=\"maps.status != null \"> and sp.status = #{maps.status}</if>\n" +
            "            <if test=\"maps.sTime != null \">  and DATE(sp.create_time) &gt;= DATE(#{maps.sTime})</if>\n" +
            "            <if test=\"maps.eTime != null \">  and DATE(sp.create_time) &lt;= DATE(#{maps.eTime})</if>\n" +
            "            <if test=\"maps.bankTransactionId != null  and maps.bankTransactionId != ''\"> and sp.bank_transaction_id = #{maps.bankTransactionId}</if>\n" +
            "            <if test=\"maps.bankSerialNo != null  and maps.bankSerialNo != ''\"> and sp.bank_serial_no = #{maps.bankSerialNo}</if>\n" +
            "            <if test=\"maps.storeId != null \"> and sp.store_id = #{maps.storeId}</if>\n" +
            "            <if test=\"maps.companyId != null \"> and sp.company_id = #{maps.companyId}</if>\n" +
            "            <if test=\"maps.companyUserId != null \"> and sp.company_user_id = #{maps.companyUserId}</if>\n" +
            "            <if test=\"maps.payMode != null  and maps.payMode != ''\"> and sp.pay_mode = #{maps.payMode}</if>\n" +
            "            <if test=\" maps.phone!= null  and maps.phone != ''\"> and u.phone = #{maps.phone}</if>\n" +
            "            <if test=\"maps.refundBeginTime != null \">  and DATE(sp.refund_time) &gt;= DATE(#{maps.refundBeginTime})</if>\n" +
            "            <if test=\"maps.refundEndTime != null \">  and DATE(sp.refund_time) &lt;= DATE(#{maps.refundEndTime})</if>\n" +
            "            <if test=\"maps.payBeginTime != null \">  and DATE(sp.pay_time) &gt;= DATE(#{maps.payBeginTime})</if>\n" +
            "            <if test=\"maps.payEndTime != null \">  and DATE(sp.pay_time) &lt;= DATE(#{maps.payEndTime})</if>\n" +
            " ORDER BY payment_id desc  "+
            "</script>"})
    List<FsStorePaymentVO> selectFsStorePaymentListVO(@Param("maps")FsStorePaymentParam fsStorePayment);
    @Select("SELECT sp.*,u.nick_name,u.phone,s.store_name FROM fs_store_payment sp LEFT JOIN  fs_user u ON u.user_id=sp.user_id LEFT JOIN fs_store s ON s.store_id=sp.store_id where payment_id=#{paymentId}")
    FsStorePaymentVO selectFsStorePaymentByPaymentIdVO(Long paymentId);
    @Select("SELECT * FROM fs_store_payment where business_id=#{orderId} and status=1 and business_type=2")
    List<FsStorePayment> selectFsStorePaymentByOrderId(Long orderId);
    @Select("SELECT * FROM fs_store_payment where business_id=#{orderId} and status=1 and business_type=1")
    List<FsStorePayment> selectFsStorePaymentByInquiryOrderId(Long orderId);
    @Select("SELECT * FROM fs_store_payment where business_id=#{orderId} and status=1 and business_type=3")
    List<FsStorePayment> selectFsStorePaymentByPackageOrderId(Long orderId);
    @Select("select * from fs_store_payment where pay_code=#{payCode}")
    FsStorePayment selectFsStorePaymentByPaymentCode(String payCode);

    List<JSONObject> selectFsStorePaymentCounts(Map<String, Object> toMap);

    @Select({"<script> " +
            "select u.nick_name, count(p.payment_id) as order_count,sum(IFNULL(p.pay_money,0)) as pay_money  from  fs_store_payment p left join company_user u on p.company_user_id=u.user_id  " +
            "where p.status = 1 " +
            "<if test = 'maps.type != null and maps.type ==1 '> " +
            "and TO_DAYS(p.create_time) = TO_DAYS(NOW()) " +
            "</if>" +
            "<if test = 'maps.type != null and maps.type ==2 '> " +
            "and TO_DAYS(NOW()) - TO_DAYS(p.create_time) &lt;= 1 " +
            "</if>" +
            "<if test = 'maps.type != null and maps.type ==3 '> " +
            "and YEARWEEK(DATE_FORMAT(p.create_time,'%Y-%m-%d')) = YEARWEEK(NOW()) " +
            "</if>" +
            "<if test = 'maps.type != null and maps.type ==4 '> " +
            "and YEARWEEK(DATE_FORMAT(p.create_time,'%Y-%m-%d')) = YEARWEEK(NOW())-1 " +
            "</if>" +
            "<if test = 'maps.type != null and maps.type ==5 '> " +
            "and DATE_FORMAT(p.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(p.create_time,'%Y%m')) = 1 " +
            "</if>" +
            "<if test = 'maps.type != null and maps.type ==7 '> " +
            "and QUARTER(p.create_time) = QUARTER(NOW()) " +
            "</if>" +
            "<if test = 'maps.type != null and maps.type ==8 '> " +
            "and QUARTER(p.create_time) = QUARTER(DATE_SUB(NOW(),INTERVAL 1 QUARTER)) " +
            "</if>" +
            "<if test = 'maps.type != null and maps.type ==9 '> " +
            "and YEAR(p.create_time)=YEAR(NOW()) " +
            "</if>" +
            "<if test = 'maps.type != null and maps.type ==10 '> " +
            "and  YEAR(p.create_time) = YEAR(DATE_SUB(NOW(),INTERVAL 1 YEAR))" +
            "</if>" +
            "<if test = 'maps.startTime != null and maps.startTime != \"\" '> " +
            "AND date_format(p.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(p.create_time,'%y%m%d') &lt;= date_format(#{maps.endTime},'%y%m%d')"+
            "</if>" +
            "and  p.`company_user_id` IN " +
            "<foreach  item='item' index='index' collection='maps.users' open='(' separator=',' close=')'> #{item}    </foreach>"+
            " group by p.company_user_id "+
            "</script>"})
    List<FsStorePaymentStatisticsVO> selectFsStorePaymentStatisticsList(@Param("maps") FsStoreStatisticsParam param);
    @Select({"<script> " +
            "select count(1) from fs_store_payment  " +
            "where business_type=1 and status=1 " +
            "<if test = 'type != null and  type ==1  '> " +
            "and  DATE_FORMAT(create_time, '%Y-%m-%d')  = DATE_FORMAT(NOW(), '%Y-%m-%d') " +
            "</if>" +
            "<if test = 'companyId != null  '> " +
            "and  company_id=#{companyId} " +
            "</if>" +
            "</script>"})
    Long selectFsStorePaymentCount(@Param("type") int type,@Param("companyId") Long companyId);
    @Select("select * from fs_store_payment where trade_no=#{tradeNo}")
    FsStorePayment selectFsStorePaymentByPaymentTradeNo(String tradeNo);
    @Select("select * from fs_store_payment where business_type=#{type} and  business_id=#{businessId} and status=1")
    List<FsStorePayment> selectFsStorePaymentByPay(@Param("type")int type,@Param("businessId") Long businessId);
    @Select("select * from fs_store_payment where business_type=#{type} and  business_id=#{businessId} and (status=1 or starus=-1)")
    List<FsStorePayment> selectFsStorePaymentByPayOrRefund(int i, Long orderId);
    @Select({"<script> " +
            " SELECT sp.*,u.nick_name,u.phone,s.store_name,c.company_name,cu.nick_name as companyUserName,fso.delivery_name,fso.package_name,fso.package_second_name FROM fs_store_payment sp " +
            " LEFT JOIN  fs_user u ON u.user_id=sp.user_id " +
            " LEFT JOIN fs_store s ON s.store_id=sp.store_id " +
            " left join fs_store_order fso on fso.order_code = sp.business_code " +
            " left join fs_inquiry_order fio on fio.order_sn = sp.business_code " +
            "LEFT JOIN company c ON \n" +
            "    ( c.company_id = fso.company_id) OR\n" +
            "    ( c.company_id = fio.company_id)\n" +
            "LEFT JOIN company_user cu ON \n" +
            "    ( cu.user_id = fso.company_user_id) OR\n" +
            "    (cu.user_id = fio.company_user_id) " +
            " where 1=1 " +
            "            <if test=\"maps.payCode != null  and maps.payCode != ''\"> and sp.pay_code = #{maps.payCode}</if>\n" +
            "            <if test=\"maps.payTypeCode != null  and maps.payTypeCode != ''\"> and sp.pay_type_code = #{maps.payTypeCode}</if>\n" +
            "            <if test=\"maps.businessCode != null  and maps.businessCode != ''\"> and sp.business_code = #{maps.businessCode}</if>\n" +
            "            <if test=\"maps.tradeNo != null  and maps.tradeNo != ''\"> and sp.trade_no = #{maps.tradeNo}</if>\n" +
            "            <if test=\"maps.userId != null \"> and sp.user_id = #{maps.userId}</if>\n" +
            "            <if test=\"maps.openId != null  and openId != ''\"> and sp.open_id = #{maps.openId}</if>\n" +
            "            <if test=\"maps.businessType != null \"> and sp.business_type = #{maps.businessType}</if>\n" +
            "            <if test=\"maps.businessId != null  and maps.businessId != ''\"> and sp.business_id = #{maps.businessId}</if>\n" +
            "            <if test=\"maps.status != null \"> and sp.status = #{maps.status}</if>\n" +
            "            <if test=\"maps.sTime != null \">  and DATE(sp.create_time) &gt;= DATE(#{maps.sTime})</if>\n" +
            "            <if test=\"maps.eTime != null \">  and DATE(sp.create_time) &lt;= DATE(#{maps.eTime})</if>\n" +
            "            <if test=\"maps.bankTransactionId != null  and maps.bankTransactionId != ''\"> and sp.bank_transaction_id = #{maps.bankTransactionId}</if>\n" +
            "            <if test=\"maps.bankSerialNo != null  and maps.bankSerialNo != ''\"> and sp.bank_serial_no = #{maps.bankSerialNo}</if>\n" +
            "            <if test=\"maps.storeId != null \"> and sp.store_id = #{maps.storeId}</if>\n" +
            "            <if test=\"maps.companyId != null \"> and sp.company_id = #{maps.companyId}</if>\n" +
            "            <if test=\"maps.companyUserId != null \"> and sp.company_user_id = #{maps.companyUserId}</if>\n" +
            "            <if test=\"maps.payMode != null  and maps.payMode != ''\"> and sp.pay_mode = #{maps.payMode}</if>\n" +
            "            <if test=\" maps.phone!= null  and maps.phone != ''\"> and u.phone = #{maps.phone}</if>\n" +
            "            <if test=\"maps.refundBeginTime != null \">  and DATE(sp.refund_time) &gt;= DATE(#{maps.refundBeginTime})</if>\n" +
            "            <if test=\"maps.refundEndTime != null \">  and DATE(sp.refund_time) &lt;= DATE(#{maps.refundEndTime})</if>\n" +
            "            <if test=\"maps.payBeginTime != null \">  and DATE(sp.pay_time) &gt;= DATE(#{maps.payBeginTime})</if>\n" +
            "            <if test=\"maps.payEndTime != null \">  and DATE(sp.pay_time) &lt;= DATE(#{maps.payEndTime})</if>\n" +
            " ORDER BY payment_id desc  "+
            "</script>"})
    List<FsStorePaymentExcelVO> selectFsStorePaymentExcelVO(@Param("maps") FsStorePaymentParam fsStorePayment);
    @Select("select * from fs_store_payment where share_status =0 and is_share=1 and `status`=1 and pay_mode='tz' and date(create_time)<'2024-02-22'")
    List<FsStorePayment> selectFsStorePaymentbyshare();

    @Select(" select  * from fs_store_payment where business_type = 2 ")
    List<FsStorePayment> selectFsStorePaymentByType();
    @Select({"<script> " +
            " SELECT count(1) FROM fs_store_payment sp " +
            " LEFT JOIN  fs_user u ON u.user_id=sp.user_id " +
            " LEFT JOIN fs_store s ON s.store_id=sp.store_id " +
            " left join fs_store_order fso on fso.order_code = sp.business_code " +
            " left join fs_inquiry_order fio on fio.order_sn = sp.business_code " +
            "LEFT JOIN company c ON \n" +
            "    ( c.company_id = fso.company_id) OR\n" +
            "    ( c.company_id = fio.company_id)\n" +
            "LEFT JOIN company_user cu ON \n" +
            "    ( cu.user_id = fso.company_user_id) OR\n" +
            "    (cu.user_id = fio.company_user_id) " +
            " where 1=1 " +
            "            <if test=\"maps.payCode != null  and maps.payCode != ''\"> and sp.pay_code = #{maps.payCode}</if>\n" +
            "            <if test=\"maps.payTypeCode != null  and maps.payTypeCode != ''\"> and sp.pay_type_code = #{maps.payTypeCode}</if>\n" +
            "            <if test=\"maps.businessCode != null  and maps.businessCode != ''\"> and sp.business_code = #{maps.businessCode}</if>\n" +
            "            <if test=\"maps.tradeNo != null  and maps.tradeNo != ''\"> and sp.trade_no = #{maps.tradeNo}</if>\n" +
            "            <if test=\"maps.userId != null \"> and sp.user_id = #{maps.userId}</if>\n" +
            "            <if test=\"maps.openId != null  and openId != ''\"> and sp.open_id = #{maps.openId}</if>\n" +
            "            <if test=\"maps.businessType != null \"> and sp.business_type = #{maps.businessType}</if>\n" +
            "            <if test=\"maps.businessId != null  and maps.businessId != ''\"> and sp.business_id = #{maps.businessId}</if>\n" +
            "            <if test=\"maps.status != null \"> and sp.status = #{maps.status}</if>\n" +
            "            <if test=\"maps.sTime != null \">  and DATE(sp.create_time) &gt;= DATE(#{maps.sTime})</if>\n" +
            "            <if test=\"maps.eTime != null \">  and DATE(sp.create_time) &lt;= DATE(#{maps.eTime})</if>\n" +
            "            <if test=\"maps.bankTransactionId != null  and maps.bankTransactionId != ''\"> and sp.bank_transaction_id = #{maps.bankTransactionId}</if>\n" +
            "            <if test=\"maps.bankSerialNo != null  and maps.bankSerialNo != ''\"> and sp.bank_serial_no = #{maps.bankSerialNo}</if>\n" +
            "            <if test=\"maps.storeId != null \"> and sp.store_id = #{maps.storeId}</if>\n" +
            "            <if test=\"maps.companyId != null \"> and sp.company_id = #{maps.companyId}</if>\n" +
            "            <if test=\"maps.companyUserId != null \"> and sp.company_user_id = #{maps.companyUserId}</if>\n" +
            "            <if test=\"maps.payMode != null  and maps.payMode != ''\"> and sp.pay_mode = #{maps.payMode}</if>\n" +
            "            <if test=\" maps.phone!= null  and maps.phone != ''\"> and u.phone = #{maps.phone}</if>\n" +
            "            <if test=\"maps.refundBeginTime != null \">  and DATE(sp.refund_time) &gt;= DATE(#{maps.refundBeginTime})</if>\n" +
            "            <if test=\"maps.refundEndTime != null \">  and DATE(sp.refund_time) &lt;= DATE(#{maps.refundEndTime})</if>\n" +
            "            <if test=\"maps.payBeginTime != null \">  and DATE(sp.pay_time) &gt;= DATE(#{maps.payBeginTime})</if>\n" +
            "            <if test=\"maps.payEndTime != null \">  and DATE(sp.pay_time) &lt;= DATE(#{maps.payEndTime})</if>\n" +
            " ORDER BY payment_id desc  "+
            "</script>"})
    Long selectFsStorePaymentExcelVOCount(@Param("maps") FsStorePaymentParam fsStorePayment);
}
