package com.fs.his.mapper;

import java.util.Date;
import java.util.List;
import com.fs.his.domain.FsStoreSubOrder;
import com.fs.his.param.FsStoreOrderParam;
import com.fs.his.vo.FsStoreSubOrderListVO;
import com.fs.his.vo.FsStoreSubOrderVO;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

/**
 * 订单Mapper接口
 *
 * @author fs
 * @date 2024-05-09
 */
public interface FsStoreSubOrderMapper
{
    /**
     * 查询订单
     *
     * @param subOrderId 订单主键
     * @return 订单
     */
    public FsStoreSubOrder selectFsStoreSubOrderBySubOrderId(Long subOrderId);

    /**
     * 查询订单列表
     *
     * @param fsStoreSubOrder 订单
     * @return 订单集合
     */
    public List<FsStoreSubOrderListVO> selectFsStoreSubOrderList(FsStoreOrderParam fsStoreSubOrder);

    /**
     * 新增订单
     *
     * @param fsStoreSubOrder 订单
     * @return 结果
     */
    public int insertFsStoreSubOrder(FsStoreSubOrder fsStoreSubOrder);

    /**
     * 修改订单
     *
     * @param fsStoreSubOrder 订单
     * @return 结果
     */
    public int updateFsStoreSubOrder(FsStoreSubOrder fsStoreSubOrder);

    /**
     * 删除订单
     *
     * @param subOrderId 订单主键
     * @return 结果
     */
    public int deleteFsStoreSubOrderBySubOrderId(String subOrderId);

    /**
     * 批量删除订单
     *
     * @param subOrderIds 需要删除的数据主键集合
     * @return 结果
     */
    public int deleteFsStoreSubOrderBySubOrderIds(String[] subOrderIds);


    @Select({"<script> " +
            "select so.*,st.store_name,us.nick_name,us.phone,c.company_name,cu.nick_name company_user_name,o.order_code,o.status order_status,o.delivery_code,o.delivery_name,o.delivery_sn,o.delivery_status,o.delivery_pay_status " +
            " FROM fs_store_sub_order so LEFT JOIN fs_store st ON so.store_id =st.store_id " +
            " left join fs_store_order o on o.order_id = so.order_id " +
            " LEFT JOIN fs_user us ON us.user_id=so.user_id " +
            " LEFT JOIN company c on c.company_id =so.company_id " +
            " LEFT JOIN company_user cu on cu.user_id=so.company_user_id  " +
            " WHERE 1=1 "+
            "            <if test=\"maps.packageSecondName != null and maps.packageSecondName != '' \"> and so.package_second_name like concat('%', #{maps.packageSecondName}, '%')</if>"+
            "            <if test=\"maps.storeId != null \"> and so.store_id = #{maps.storeId}</if>\n" +
            "            <if test=\"maps.subOrderCode != null  and maps.subOrderCode != ''\"> and so.sub_order_code = #{maps.subOrderCode}</if>\n" +
            "            <if test=\"maps.userName != null  and maps.userName != ''\"> and so.user_name like concat('%', #{maps.userName}, '%')</if>\n" +
            "            <if test=\"maps.userPhone != null  and maps.userPhone != ''\"> and so.user_phone = #{maps.userPhone}</if>\n" +
            "            <if test=\"maps.userId != null  \"> and so.user_id = #{maps.userId}</if>\n" +
            "            <if test=\"maps.isFirst != null  \"> and so.is_first = #{maps.isFirst}</if>\n" +
            "            <if test=\"maps.status != null \"> and o.status = #{maps.status}</if>\n" +
            "            <if test=\"maps.deliverySn != null  and maps.deliverySn != ''\"> and o.delivery_sn = #{maps.deliverySn}</if>\n" +
            "            <if test=\"maps.shippingType != null \"> and so.shipping_type = #{maps.shippingType}</if>\n" +
            "            <if test=\"maps.isChannel != null  and maps.isChannel != ''\"> and so.is_channel = #{maps.isChannel}</if>\n" +
            "            <if test=\"maps.isPrescribe != null \"> and so.is_prescribe = #{maps.isPrescribe}</if>\n" +
            "            <if test=\"maps.prescribeId != null \"> and so.prescribe_id = #{maps.prescribeId}</if>\n" +
            "            <if test=\"maps.companyUserId != null \"> and so.company_user_id = #{maps.companyUserId}</if>\n" +
            "            <if test=\"maps.sTime != null \">  and DATE(so.create_time) &gt;= DATE(#{maps.sTime})</if>\n" +
            "            <if test=\"maps.eTime != null \">  and DATE(so.create_time) &lt;= DATE(#{maps.eTime})</if>\n" +
            "            <if test=\"maps.paysTime != null \">  and DATE(so.pay_time) &gt;= DATE(#{maps.paysTime})</if>\n" +
            "            <if test=\"maps.payeTime != null \">  and DATE(so.pay_time) &lt;= DATE(#{maps.payeTime})</if>\n" +
            "            <if test=\"maps.orderCreateType != null \"> and so.order_create_type = #{maps.orderCreateType}</if>"+
            "            <if test=\"maps.orderType != null \"> and so.order_type = #{maps.orderType}</if>" +
            "            <if test=\"maps.companyUserNickName != null and  maps.companyUserNickName !='' \"> and cu.nick_name like concat( #{maps.companyUserNickName}, '%')"+
            "</if>" +
            "<if test = 'maps.companyId != null  and   maps.companyId != \"-1\" '> " +
            "and so.company_id =#{maps.companyId} " +
            "</if>" +
            "<if test = ' maps.companyId == \"-1\" '> " +
            "and so.company_id is null " +
            "</if>" +
            "<if test = 'maps.deliveryStatus != null    '> " +
            "and so.delivery_status =#{maps.deliveryStatus} " +
            "</if>" +
            "<if test = 'maps.deptId != null    '> " +
            "  AND (so.dept_id = #{maps.deptId} OR so.dept_id IN ( SELECT t.dept_id FROM company_dept t WHERE find_in_set(#{maps.deptId}, ancestors) )) " +
            "</if>" +
            "<if test=\"maps.packageName != null and maps.packageName != '' \"> and so.package_name like concat('%', #{maps.packageName}, '%')</if> " +
            " ORDER BY so.sub_order_id desc "+
            "</script>"})
    List<FsStoreSubOrderListVO> selectFsStoreOrderListVO(@Param("maps") FsStoreOrderParam fsStoreOrder);
    @Select("select so.*,st.store_name,us.nick_name,us.phone,c.company_name,cu.nick_name company_user_name,o.order_code,o.status order_status,o.delivery_code,o.delivery_name,o.delivery_sn,o.delivery_status,o.delivery_pay_status FROM fs_store_sub_order so LEFT JOIN fs_store st ON so.store_id =st.store_id left join fs_store_order o on o.order_id = so.order_id LEFT JOIN fs_user us ON us.user_id=so.user_id LEFT JOIN company c on c.company_id =so.company_id LEFT JOIN company_user cu on cu.user_id=so.company_user_id  where so.sub_order_id=#{subOrderId}")
    FsStoreSubOrderVO selectFsStoreSubOrderVOBySubOrderId(Long subOrderId);
    @Select("SELECT count(1) FROM fs_store_sub_order WHERE package_name = #{packageName} and patient_idcard=#{idCard} AND create_time BETWEEN DATE_SUB(#{createTime}, INTERVAL 1 MONTH) AND DATE_ADD(#{createTime}, INTERVAL 1 MONTH) ")
    int selectFsStoreSubOrderByPackageNameAndId(@Param("packageName") String packageName, @Param("idCard") String idCard, @Param("createTime") Date createTime);
    @Select("select sub_order_id from fs_store_sub_order WHERE  is_push=0 and  DATE(create_time)= CURDATE() and cycle>0 and cycle<30 and pay_price>10 ORDER BY sub_order_id desc  ")
    List<Long> selectFsStoreSubOrderByNoPush();
    @Select({"<script> select count(1) form fs_store_sub_order" +
            "<where>\n" +
            "            <if test=\"orderId != null \"> and order_id = #{orderId}</if>\n" +
            "            <if test=\"storeId != null \"> and store_id = #{storeId}</if>\n" +
            "            <if test=\"subOrderCode != null  and subOrderCode != ''\"> and sub_order_code = #{subOrderCode}</if>\n" +
            "            <if test=\"userId != null \"> and user_id = #{userId}</if>\n" +
            "            <if test=\"userName != null  and userName != ''\"> and user_name like concat('%', #{userName}, '%')</if>\n" +
            "            <if test=\"userPhone != null  and userPhone != ''\"> and user_phone = #{userPhone}</if>\n" +
            "            <if test=\"userAddress != null  and userAddress != ''\"> and user_address = #{userAddress}</if>\n" +
            "            <if test=\"totalNum != null \"> and total_num = #{totalNum}</if>\n" +
            "            <if test=\"totalPrice != null \"> and total_price = #{totalPrice}</if>\n" +
            "            <if test=\"payPrice != null \"> and pay_price = #{payPrice}</if>\n" +
            "            <if test=\"payMoney != null \"> and pay_money = #{payMoney}</if>\n" +
            "            <if test=\"payRemain != null \"> and pay_remain = #{payRemain}</if>\n" +
            "            <if test=\"payTime != null \"> and pay_time = #{payTime}</if>\n" +
            "            <if test=\"payType != null  and payType != ''\"> and pay_type = #{payType}</if>\n" +
            "            <if test=\"prescribePrice != null \"> and prescribe_price = #{prescribePrice}</if>\n" +
            "            <if test=\"totalCostPrice != null \"> and total_cost_price = #{totalCostPrice}</if>\n" +
            "            <if test=\"inquiryCostPrice != null \"> and inquiry_cost_price = #{inquiryCostPrice}</if>\n" +
            "            <if test=\"orderType != null \"> and order_type = #{orderType}</if>\n" +
            "            <if test=\"status != null \"> and status = #{status}</if>\n" +
            "            <if test=\"payDelivery != null \"> and pay_delivery = #{payDelivery}</if>\n" +
            "            <if test=\"finishTime != null \"> and finish_time = #{finishTime}</if>\n" +
            "            <if test=\"companyId != null \"> and company_id = #{companyId}</if>\n" +
            "            <if test=\"companyUserId != null \"> and company_user_id = #{companyUserId}</if>\n" +
            "            <if test=\"deptId != null \"> and dept_id = #{deptId}</if>\n" +
            "            <if test=\"packageId != null \"> and package_id = #{packageId}</if>\n" +
            "            <if test=\"doctorId != null \"> and doctor_id = #{doctorId}</if>\n" +
            "            <if test=\"inquiryOrderId != null \"> and inquiry_order_id = #{inquiryOrderId}</if>\n" +
            "            <if test=\"packageOrderId != null \"> and package_order_id = #{packageOrderId}</if>\n" +
            "            <if test=\"cycle != null \"> and cycle = #{cycle}</if>\n" +
            "            <if test=\"followDoctorId != null \"> and follow_doctor_id = #{followDoctorId}</if>\n" +
            "            <if test=\"followNum != null \"> and follow_num = #{followNum}</if>\n" +
            "            <if test=\"packageName != null  and packageName != ''\"> and package_name like concat('%', #{packageName}, '%')</if>\n" +
            "            <if test=\"packageSecondName != null  and packageSecondName != ''\"> and package_second_name like concat('%', #{packageSecondName}, '%')</if>\n" +
            "            <if test=\"icdCode != null  and icdCode != ''\"> and icd_code = #{icdCode}</if>\n" +
            "            <if test=\"prescribeJson != null  and prescribeJson != ''\"> and prescribe_json = #{prescribeJson}</if>\n" +
            "            <if test=\"prescribeDrugJson != null  and prescribeDrugJson != ''\"> and prescribe_drug_json = #{prescribeDrugJson}</if>\n" +
            "            <if test=\"diagnosis != null  and diagnosis != ''\"> and diagnosis = #{diagnosis}</if>\n" +
            "            <if test=\"patientId != null \"> and patient_id = #{patientId}</if>\n" +
            "            <if test=\"patientName != null  and patientName != ''\"> and patient_name like concat('%', #{patientName}, '%')</if>\n" +
            "            <if test=\"patientIdcard != null  and patientIdcard != ''\"> and patient_idcard = #{patientIdcard}</if>\n" +
            "            <if test=\"detailsUsage != null  and detailsUsage != ''\"> and details_usage = #{detailsUsage}</if>\n" +
            "            <if test=\"isPush != null  and isPush != ''\"> and is_push = #{isPush}</if>\n" +
            "            <if test=\"prescriptionNum != null \"> and prescription_num = #{prescriptionNum}</if>\n" +
            "            <if test=\"prescriptionTaking != null  and prescriptionTaking != ''\"> and prescription_taking = #{prescriptionTaking}</if>\n" +
            "            <if test=\"prescriptionRequirements != null  and prescriptionRequirements != ''\"> and prescription_requirements = #{prescriptionRequirements}</if>\n" +
            "        </where> "+
            "</script>"})
    Long selectFsStoreSubOrderListCount(FsStoreOrderParam fsStoreSubOrder);
}
