package com.fs.his.mapper;

import java.util.List;

import com.fs.his.domain.FsPackage;
import com.fs.his.domain.FsPackageOrder;
import com.fs.his.domain.FsStorePayment;
import com.fs.his.param.FsPackageOrderListUParam;
import com.fs.his.param.FsPackageOrderParam;
import com.fs.his.vo.FsPackageOrderExcelVO;
import com.fs.his.vo.FsPackageOrderListUVO;
import com.fs.his.vo.FsPackageOrderListVO;
import com.fs.his.vo.FsPackageOrderVO;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

/**
 * 套餐订单Mapper接口
 *
 * @author fs
 *
 *
 *
 * @date 2023-08-25
 */
public interface FsPackageOrderMapper
{
    /**
     * 查询套餐订单
     *
     * @param orderId 套餐订单主键
     * @return 套餐订单
     */
    public FsPackageOrder selectFsPackageOrderByOrderId(Long orderId);

    /**
     * 查询套餐订单列表
     *
     * @param fsPackageOrder 套餐订单
     * @return 套餐订单集合
     */
    public List<FsPackageOrder> selectFsPackageOrderList(FsPackageOrder fsPackageOrder);

    /**
     * 新增套餐订单
     *
     * @param fsPackageOrder 套餐订单
     * @return 结果
     */
    public int insertFsPackageOrder(FsPackageOrder fsPackageOrder);

    /**
     * 修改套餐订单
     *
     * @param fsPackageOrder 套餐订单
     * @return 结果
     */
    public int updateFsPackageOrder(FsPackageOrder fsPackageOrder);

    /**
     * 删除套餐订单
     *
     * @param orderId 套餐订单主键
     * @return 结果
     */
    public int deleteFsPackageOrderByOrderId(Long orderId);

    /**
     * 批量删除套餐订单
     *
     * @param orderIds 需要删除的数据主键集合
     * @return 结果
     */
    public int deleteFsPackageOrderByOrderIds(Long[] orderIds);

    @Select({"<script> " +
            "select o.order_id,o.package_second_name,o.source,o.order_sn,o.package_name,o.pay_time, o.days,o.pay_price,o.pay_money,o.pay_type,o.`status`,o.package_sub_type,o.create_time,o.start_time,o.finish_time      ,d.doctor_name,u.nick_name,u.phone,c.company_name,cu.nick_name company_user_name,patient_json->>'$.patientName' as patientName,fso.delivery_status,fso.delivery_pay_status  from fs_package_order o LEFT JOIN fs_doctor d ON d.doctor_id=o.doctor_id LEFT JOIN fs_user u ON u.user_id=o.user_id LEFT JOIN company c on c.company_id =o.company_id LEFT JOIN company_user cu on cu.user_id=o.company_user_id LEFT JOIN fs_store_order fso ON fso.order_id= o.store_order_id"+
            " where 1 = 1 \n" +
            "            <if test=\"maps.orderSn != null  and maps.orderSn != ''\"> and o.order_sn = #{maps.orderSn}</if>\n" +
            "            <if test=\"maps.phone != null and maps.phone != '' \">and u.phone = #{maps.phone} </if>\n" +
            "            <if test=\"maps.doctorName != null \">and d.doctor_name like concat('%', #{maps.doctorName}, '%')</if>\n" +
            "            <if test=\"maps.packageName != null  and maps.packageName != ''\"> and o.package_name like concat('%', #{maps.packageName}, '%')</if>\n" +
            "            <if test=\"maps.isPay != null \"> and o.is_pay = #{maps.isPay}</if>\n" +
            "            <if test=\"maps.packageSubType != null \"> and o.package_sub_type = #{maps.packageSubType}</if>\n" +
            "            <if test=\"maps.userName != null and maps.userName != '' \"> and fso.user_name like concat('%', #{maps.userName}, '%')</if>\n" +
            "            <if test=\"maps.userPhone != null and maps.userPhone != '' \"> and fso.user_phone = #{maps.userPhone}</if>\n" +
            "            <if test=\"maps.status != null \"> and o.status = #{maps.status}</if>\n" +
            "            <if test=\"maps.source != null \"> and o.source = #{maps.source}</if>\n" +
            "            <if test=\"maps.sTime != null \">  and DATE(o.start_time) &gt;= DATE(#maps.sTime})</if>\n" +
            "            <if test=\"maps.eTime != null \">  and DATE(o.start_time) &lt;= DATE(#{maps.eTime})</if>\n" +
            "            <if test=\"maps.stTime != null \">  and DATE(o.create_time) &gt;= DATE(#{maps.stTime})</if>\n" +
            "            <if test=\"maps.endTime != null \">  and DATE(o.create_time) &lt;= DATE(#{maps.endTime})</if>\n" +
            "            <if test=\"maps.endStartTime != null \">  and DATE(o.finish_time) &gt;= DATE(#{maps.endStartTime})</if>\n" +
            "            <if test=\"maps.endEndTime != null \">  and DATE(o.finish_time) &lt;= DATE(#{maps.endEndTime})</if>\n" +
            "            <if test=\"maps.companyUserId != null \"> and o.company_user_id = #{maps.companyUserId}</if>\n" +
            "            <if test=\"maps.companyName != null \"> and c.company_name like concat('%', #{maps.companyName}, '%')</if>\n" +
            "            <if test=\"maps.companyUserName != null \"> and cu.user_name like concat('%', #{maps.companyUserName}, '%')</if>\n" +
            "            <if test = 'maps.companyId != null  and   maps.companyId != \"-1\" '> and o.company_id =#{maps.companyId} </if>" +
            "            <if test = ' maps.companyId == \"-1\" '> and o.company_id is null </if>" +
            "            <if test = 'maps.deptId != null    '> AND (o.dept_id = #{maps.deptId} OR o.dept_id IN ( SELECT t.dept_id FROM company_dept t WHERE find_in_set(#{maps.deptId}, ancestors) )) </if>" +
            " ${maps.params.dataScope} "+
            " order by  order_id desc "+
            "</script>"})
    List<FsPackageOrderListVO> selectFsPackageOrderListVO(@Param("maps")FsPackageOrderParam fsPackageOrder);

    @Select("select o.*,d.doctor_name,u.nick_name,u.phone,c.company_name,cu.nick_name company_user_name,fp.second_name,o.patient_json->>'$.patientName' as patientName  " +
            " from fs_package_order o " +
            " LEFT JOIN fs_doctor d ON d.doctor_id=o.doctor_id " +
            " LEFT JOIN fs_user u ON u.user_id=o.user_id " +
            " LEFT JOIN company c on c.company_id =o.company_id " +
            " LEFT JOIN company_user cu on cu.user_id=o.company_user_id " +
            " left join fs_package fp on fp.package_id = o.package_id  " +
            " where o.order_id=#{orderId}")
    FsPackageOrderVO selectFsPackageOrderByOrderIdVO(Long orderId);

    @Select("select * from fs_package_order where order_sn=#{orderSn} ")
    FsPackageOrder selectFsPackageOrderByOrderSn(String orderSn);

    @Select({"<script>" +
            "select o.order_id,o.order_sn,o.pay_price,o.status,o.package_name,json_unquote(json_extract(o.package_json,'$.imgUrl')) AS img_url,o.package_json -> '$.cycle' AS cycle,o.package_json -> '$.duration' AS duration  from fs_package_order o    " +
            "where 1=1  " +
            "<if test = ' maps.userId != null '> " +
            "and o.user_id = #{maps.userId} " +
            "</if>" +
            "<if test = ' maps.companyUserId != null '> " +
            "and o.company_user_id = #{maps.companyUserId} " +
            "</if>" +
            "<if test = 'maps.status != null and maps.status!=0    '> " +
            "and o.status = #{maps.status} " +
            "</if>" +
            " order by o.create_time desc "+
            "</script>"})
    List<FsPackageOrderListUVO> selectFsPackageOrderListUVO(@Param("maps")FsPackageOrderListUParam param);

    @Select("SELECT * FROM fs_store_payment where business_id=#{orderId} and status=1 and business_type=3")
    List<FsStorePayment> selectFsPackagePaymentByOrderId(Long orderId);
    @Select({"<script> " +
            "select o.order_id,o.order_sn,o.package_name,o.source,o.pay_time,o.follow_rate,o.cycle,o.follow_num,o.inquiry_cost_price,o.product_cost_price,o.total_cost_price,o.cost_discount_money,o.total_price,o.discount_money,o.pay_delivery,o.is_pay,o.remark, o.days,o.pay_price,o.pay_money,o.pay_type,o.`status`,o.package_sub_type,o.create_time,o.start_time,o.refund_status,o.finish_time,d.doctor_name,u.nick_name,u.phone,c.company_name,cu.nick_name company_user_name,patient_json->>'$.patientName' as patientName,fso.delivery_status,fso.delivery_pay_status from fs_package_order o LEFT JOIN fs_doctor d ON d.doctor_id=o.doctor_id LEFT JOIN fs_user u ON u.user_id=o.user_id LEFT JOIN company c on c.company_id =o.company_id LEFT JOIN company_user cu on cu.user_id=o.company_user_id LEFT JOIN fs_store_order fso ON fso.order_id= o.store_order_id "+
            "  <where>  \n" +
            "            <if test=\"orderSn != null  and orderSn != ''\"> and o.order_sn =#{orderSn}</if>\n" +
            "            <if test=\"phone != null \">and u.phone like concat( #{phone}, '%')</if>\n" +
            "            <if test=\"doctorName != null \">and d.doctor_name like concat(#{doctorName}, '%')</if>\n" +
            "            <if test=\"packageName != null  and packageName != ''\"> and o.package_name like concat('%', #{packageName}, '%')</if>\n" +
            "            <if test=\"isPay != null \"> and o.is_pay = #{isPay}</if>\n" +
            "            <if test=\"status != null \"> and o.status = #{status}</if>\n" +
            "            <if test=\"source != null \"> and o.source = #{source}</if>\n" +
            "            <if test=\"userName != null and userName != '' \"> and fso.user_name like concat('%', #{userName}, '%')</if>\n" +
            "            <if test=\"userPhone != null and userPhone != '' \"> and fso.user_phone = #{userPhone}</if>\n" +
            "            <if test=\"sTime != null \">  and DATE(o.start_time) &gt;= DATE(#{sTime})</if>\n" +
            "            <if test=\"eTime != null \">  and DATE(o.start_time) &lt;= DATE(#{eTime})</if>\n" +
            "            <if test=\"stTime != null \">  and DATE(o.create_time) &gt;= DATE(#{stTime})</if>\n" +
            "            <if test=\"endTime != null \">  and DATE(o.create_time) &lt;= DATE(#{endTime})</if>\n" +
            "            <if test=\"endStartTime != null \">  and DATE(o.finish_time) &gt;= DATE(#{endStartTime})</if>\n" +
            "            <if test=\"endEndTime != null \">  and DATE(o.finish_time) &lt;= DATE(#{endEndTime})</if>\n" +
            "<if test = 'companyId != null  and   companyId != \"-1\" '> " +
            "and o.company_id =#{companyId} " +
            "</if>" +
            "<if test = ' companyId == \"-1\" '> " +
            "and o.company_id is null " +
            "</if>" +
            "            <if test=\"companyUserId != null \"> and o.company_user_id = #{companyUserId}</if>\n" +
            "            <if test=\"companyName != null \"> and c.company_name like concat('%', #{companyName}, '%')</if>\n" +
            "            <if test=\"companyUserName != null \"> and cu.user_name like concat('%', #{companyUserName}, '%')</if>\n" +
            "<if test = 'deptId != null    '> " +
            "  AND (o.dept_id = #{deptId} OR o.dept_id IN ( SELECT t.dept_id FROM company_dept t WHERE find_in_set(#{deptId}, ancestors) )) " +
            "</if>" +
            "        </where>"+
            "   order by  order_id desc    "+
            "</script>"})
    List<FsPackageOrderExcelVO> selectFsPackageOrderExcelListVO(FsPackageOrderParam fsPackageOrder);
    @Select("select * from fs_package_order WHERE `status`=2 AND package_sub_type!=1 and DATE(follow_time) = CURDATE() ")
    List<FsPackageOrder> selectFsPackageOrderIdByFollow();
    @Update("UPDATE fs_package_order SET `status`=3 WHERE `status`=2 and DATE(finish_time)<DATE(NOW())")
    void updatePackageOrderStatusByFinishTime();
    @Select("select follow_doctor_id from fs_store_order  WHERE  follow_doctor_id is NOT NULL and user_id=#{id}  order by create_time desc LIMIT 1")
    Long selectFsPackageOrderByUserId(Long id);
    @Update("UPDATE fs_package_order SET `status`=-1 WHERE `status`=1 and create_time < DATE_SUB(NOW(), INTERVAL #{unPayTime} MINUTE)")
    Long updatePackageOrderStatusByStatus(Integer unPayTime);
    @Select("select order_id from fs_package_order ")
    List<FsPackageOrder> selectFsPackageOrderallList();
    @Select("SELECT * FROM fs_package WHERE product_json LIKE CONCAT('%', #{productId}, '%')")
    List<FsPackage> selectFsPackageOrderListByProd(String productId);
    @Select("select ifnull(count(1),0) from fs_package_order where user_id=#{userId}  and status =#{status} ")
    Integer selectFsPackageOrderCount(@Param("status") Integer status,@Param("userId") Long userId);
    @Select({"<script> " +
            "select count(1) from fs_package_order o LEFT JOIN fs_doctor d ON d.doctor_id=o.doctor_id LEFT JOIN fs_user u ON u.user_id=o.user_id LEFT JOIN company c on c.company_id =o.company_id LEFT JOIN company_user cu on cu.user_id=o.company_user_id LEFT JOIN fs_store_order fso ON fso.order_id= o.store_order_id "+
            "  <where>  \n" +
            "            <if test=\"orderSn != null  and orderSn != ''\"> and o.order_sn =#{orderSn}</if>\n" +
            "            <if test=\"phone != null \">and u.phone like concat( #{phone}, '%')</if>\n" +
            "            <if test=\"doctorName != null \">and d.doctor_name like concat( #{doctorName}, '%')</if>\n" +
            "            <if test=\"packageName != null  and packageName != ''\"> and o.package_name like concat('%', #{packageName}, '%')</if>\n" +
            "            <if test=\"isPay != null \"> and o.is_pay = #{isPay}</if>\n" +
            "            <if test=\"status != null \"> and o.status = #{status}</if>\n" +
            "            <if test=\"source != null \"> and o.source = #{source}</if>\n" +
            "            <if test=\"userName != null and userName != '' \"> and fso.user_name like concat('%', #{userName}, '%')</if>\n" +
            "            <if test=\"userPhone != null and userPhone != '' \"> and fso.user_phone = #{userPhone}</if>\n" +
            "            <if test=\"sTime != null \">  and DATE(o.start_time) &gt;= DATE(#{sTime})</if>\n" +
            "            <if test=\"eTime != null \">  and DATE(o.start_time) &lt;= DATE(#{eTime})</if>\n" +
            "            <if test=\"stTime != null \">  and DATE(o.create_time) &gt;= DATE(#{stTime})</if>\n" +
            "            <if test=\"endTime != null \">  and DATE(o.create_time) &lt;= DATE(#{endTime})</if>\n" +
            "            <if test=\"endStartTime != null \">  and DATE(o.finish_time) &gt;= DATE(#{endStartTime})</if>\n" +
            "            <if test=\"endEndTime != null \">  and DATE(o.finish_time) &lt;= DATE(#{endEndTime})</if>\n" +
            "<if test = 'companyId != null  and   companyId != \"-1\" '> " +
            "and o.company_id =#{companyId} " +
            "</if>" +
            "<if test = ' companyId == \"-1\" '> " +
            "and o.company_id is null " +
            "</if>" +
            "            <if test=\"companyUserId != null \"> and o.company_user_id = #{companyUserId}</if>\n" +
            "            <if test=\"companyName != null \"> and c.company_name like concat('%', #{companyName}, '%')</if>\n" +
            "            <if test=\"companyUserName != null \"> and cu.user_name like concat('%', #{companyUserName}, '%')</if>\n" +
            "<if test = 'deptId != null    '> " +
            "  AND (o.dept_id = #{deptId} OR o.dept_id IN ( SELECT t.dept_id FROM company_dept t WHERE find_in_set(#{deptId}, ancestors) )) " +
            "</if>" +
            "        </where>"+
            " "+
            "</script>"})
    Long selectFsPackageOrderExcelListVOCount(FsPackageOrderParam fsPackageOrder);

    FsPackage selectFsPackageByOrderId(Long packageOrderId);
}
