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 org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Component;

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);

    /**
     * 更新已完成订单
     * @return
     */
    int updateCompleteOrder();
}
