package com.winsun.mapper;

import com.baomidou.mybatisplus.mapper.BaseMapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.winsun.bean.BroadBandOrder;
import com.winsun.bean.Order;
import com.winsun.bean.OrderView;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestParam;

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

@Mapper
@Component
public interface OrderMapper extends BaseMapper<Order> {


    @Insert("insert into `hhr_order_history` values(#{id},#{status},#{date},#{operator}) ")
    Integer inserOrderHis(@Param("id") String id, @Param("status") String status, @Param("date") Date date, @Param("operator") String operator);


    @Select("<script>SELECT * FROM `hhr_order` <where> ${ew.sqlSegment} </where> </script>")
    List<Map<String, Object>> downloadOrder(@Param("ew") Wrapper<Order> wrapper);

    @Select("select *  from `hhr_order_history` where  order_id = #{oredrId}")
    List<Map<String,Object>> selectOrderHis(String oredrId);

    @Update("update `hhr_order` set order_status = #{orderStatus}, accept_name = #{acceptName},success_time=#{successTime}," +
            " accept_result = #{acceptResult}, accept_comment = #{acceptComment}, accept_date = #{acceptDate} " +
            " where order_number = #{orderNumber}")
    Integer updateOrderSUByOrderNumberIsNotNull(@Param("orderNumber") String orderNumber,@Param("orderStatus") String orderStatus,
                              @Param("acceptName") String acceptName,@Param("acceptResult") String acceptResult,
                              @Param("acceptComment") String acceptComment,@Param("acceptDate") Date acceptDate,@Param("successTime")Date successTime);

    @Update("update `hhr_order` set order_status = #{orderStatus}, accept_name = #{acceptName}," +
            " accept_result = #{acceptResult}, accept_date = #{acceptDate},success_time=#{successTime} " +
            " where order_number = #{orderNumber}")
    Integer updateOrderSUByOrderNumberIsNull(@Param("orderNumber") String orderNumber,@Param("orderStatus") String orderStatus,
                                                @Param("acceptName") String acceptName,@Param("acceptResult") String acceptResult,
                                                @Param("acceptDate") Date acceptDate,@Param("successTime")Date successTime);

    @Update("update `hhr_order` set order_status = #{orderStatus},company = #{company},kuaidi_order = #{kuaidiOrder},send_type = #{sendType}  where order_number = #{orderNumber}")
    Integer updateOrderKDNotNull(@Param("orderNumber") String orderNumber,@Param("company") String company,@Param("kuaidiOrder") String kuaidiOrder,@Param("sendType") String sendType,@Param("orderStatus") String orderStatus);

    @Update("update `hhr_order` set order_status = #{orderStatus},company = #{company},kuaidi_order = #{kuaidiOrder},send_type = #{sendType},success_time=#{successTime}  where order_number = #{orderNumber}")
    Integer updateOrderKDIsNotNullByTime(@Param("orderNumber") String orderNumber,@Param("company") String company,@Param("kuaidiOrder") String kuaidiOrder,@Param("sendType") String sendType,@Param("orderStatus") String orderStatus,@Param("successTime")Date successTime);


    @Update("update `hhr_order` set order_status = #{orderStatus},send_type = #{sendType}  where order_number = #{orderNumber}")
    Integer updateOrderKDNull(@Param("orderNumber") String orderNumber,@Param("sendType") String sendType,@Param("orderStatus") String orderStatus);

    @Update("update `hhr_order` set order_status = #{orderStatus},send_type = #{sendType},success_time=#{successTime}  where order_number = #{orderNumber}")
    Integer updateOrderKDIsNullByTime(@Param("orderNumber") String orderNumber,@Param("sendType") String sendType,@Param("orderStatus") String orderStatus,@Param("successTime")Date successTime);

    @Select("select id  from `hhr_order` where  order_number = #{orderNumber}")
    String selectIdByOrderNumber(String orderNumber);

    @Select("select xb_order_id xbOrderId  from `hhr_order` where  order_number = #{orderNumber}")
    String selectxbOrderIdByOrderNumber(String orderNumber);

    List<Order> selectOrderIdByOrderNumber(@Param("orderNumber") String orderNumber);

    @Select(" SELECT id,order_status,hehuoren_id,package_id,customer_name,business_number, " +
            " contact_number,site,address,id_card,parent_name,business_iccid,kapin,id_cardz,id_cardf,id_cardzs,studen_card, " +
            " create_time FROM hhr_order where id= #{id} ")
    Map<String, Object> selectOrderById(String id);

    @Select("SELECT u.*,s.service_qrcode,s.kdsl,s.net_type,s.is_xb,s.jilt_kd,s.jilt_kdy,s.net_phone " +
            "FROM hhr_user u LEFT JOIN hhr_user_school us ON u.id=us.user_id " +
            "LEFT JOIN hhr_school s ON us.school_id=s.id WHERE u.id= #{hehuoren_id} ")
    Map<String, Object> findPartnerById(String hehuoren_id);

    @Select("SELECT * FROM hhr_ysm_user WHERE id = #{id}")
    Map<String, Object> findYsmById(int id);

    @Select("select ysm from `hhr_user` where  id = #{id}")
    String selectYsmById(String id);

    @Select("SELECT * FROM hhr_order WHERE id = #{id}")
    List<Map<String,Object>> findHhrOrderId(String id);

    @Select("select * from product_manage where product_title = #{productTitle} and product_region = #{universityRegion} and product_university = #{universityName} and product_state=0")
    List<Map<String,Object>> queExiteProductNameData(String productTitle, String universityRegion, String universityName);

    @Select("select * from university_info where university_name = #{universityName} and university_region =#{universityRegion}")
    List<Map<String,Object>> queUniversityNameData(String universityName, String universityRegion);

    @Select("select ui.* from product_manage pm " +
            " join university_info ui on ui.university_id = pm.university_id " +
            " and product_title = #{productTitle} and product_region = #{universityRegion} and product_university = #{universityName} and product_state=0 ")
    List<Map<String,Object>> queProductNameDataToUniversityNameData(String productTitle, String universityRegion, String universityName);

    List<BroadBandOrder> selectBroadBandOrderList(Page<BroadBandOrder> page, BroadBandOrder broadBandOrder);

    /**
     * 查询重复订单
     * @param idCard
     * @return
     */
    List<Order> selectRepeatOrder(String idCard);

    /**
     * 查询宽带清单
     * @param accountNumber
     * @return
     */
    List<Map<String, Object>> selectBroadList(String accountNumber);

    /**
     * 查询宽带订单
     * @param KdOrderID
     * @return
     */
    Map<String, Object> selectKdOrder(String KdOrderID);

    /**
     * 查询多媒体账号在甲方提供清单中是否存在
     * @param orderSeq
     * @return
     */
    List<OrderView> selectReportOrder(String orderSeq);
    /**
     * 查询多媒体账号在甲方提供清单中是否存在
     * @param accountNumber
     * @return
     */
    List<Map<String, Object>> checkOldKdUser(String accountNumber);
    /**
     * 查询多媒体账号在订单表是否存在
     * @param accountNumber
     * @return
     */
    List<Map<String, Object>> checkOldhhrOrder(String accountNumber);
    /**
     * 更新已完成订单
     * @return
     */
    int updateCompleteOrder();

    /**
     * 更新未下单并且融合时间小于当前时间的订单
     * @return
     */
    int updateNosendAndRhTimeLtNowOrder();

    @Select("select * from bys_student where phone = #{phone}")
    List<Map<String, Object>> selectBys(String phone);

    /**
     * 查询是否存在预存送流量订单
     * */
    @Select("SELECT * FROM hhr_order WHERE contact_number = #{phone} and business_package = '预存送流量'")
    List<Map<String, Object>> selectOrderYcsll(@Param("phone") String phone);
}
