package cn.com.ty.lift.ud.chat.mapper;

import cn.com.ty.lift.ud.chat.controller.query.LiftCaseQuery;
import cn.com.ty.lift.ud.chat.mapper.entity.LiftCaseEntity;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.math.BigDecimal;
import java.util.List;

/**
 * 诊单 -Repository
 * 
 * @author Xson
 * @date 2020-04-23
 */
public interface LiftCaseMapper extends BaseMapper<LiftCaseEntity> {

    /**
     * This method was generated by MyBatis Generator. This method corresponds to the database table lift_case
     *
     * @mbggenerated Thu May 07 14:53:11 CST 2020
     */
    int deleteByPrimaryKey(Integer id);

    /**
     * This method was generated by MyBatis Generator. This method corresponds to the database table lift_case
     *
     * @mbggenerated Thu May 07 14:53:11 CST 2020
     */
    int insertSelective(LiftCaseEntity record);

    /**
     * This method was generated by MyBatis Generator. This method corresponds to the database table lift_case
     *
     * @mbggenerated Thu May 07 14:53:11 CST 2020
     */
    LiftCaseEntity selectByPrimaryKey(Integer id);

    /**
     * This method was generated by MyBatis Generator. This method corresponds to the database table lift_case
     *
     * @mbggenerated Thu May 07 14:53:11 CST 2020
     */
    int updateByPrimaryKeySelective(LiftCaseEntity record);

    /**
     * This method was generated by MyBatis Generator. This method corresponds to the database table lift_case
     *
     * @mbggenerated Thu May 07 14:53:11 CST 2020
     */
    int updateByPrimaryKey(LiftCaseEntity record);

    List<LiftCaseEntity> selectByQuery(@Param("page") LiftCaseQuery query);
    int countByQuery(@Param("page") LiftCaseQuery query);
    /**
     * 查询用户全部诊单
     * @param query
     * @return
     */
    List<LiftCaseEntity> queryUserAll(@Param("query") LiftCaseQuery query);
    int queryUserAllCount(@Param("query") LiftCaseQuery query);

    /**
     * 查询专家全部诊单
     * @param query
     * @return
     */
    List<LiftCaseEntity> queryChargeAll(@Param("query") LiftCaseQuery query);
    int queryChargeAllCount(@Param("query") LiftCaseQuery query);
    /**
     * 查询用户代付款
     * @param query
     * @return
     */
    List<LiftCaseEntity> queryToPay(@Param("query") LiftCaseQuery query);
    int queryToPayCount(@Param("query") LiftCaseQuery query);
    /**
     * 查询用户待确认
     * @param query
     * @return
     */
    List<LiftCaseEntity> queryUserToSure(@Param("query") LiftCaseQuery query);
    int queryUserToSureCount(@Param("query") LiftCaseQuery query);
    /**
     * 查询用户待评价
     * @param query
     * @return
     */
    List<LiftCaseEntity> queryToEva(@Param("query") LiftCaseQuery query);
    int queryToEvaCount(@Param("query") LiftCaseQuery query);
    /**
     * 查询专家待报价
     * @param query
     * @return
     */
    List<LiftCaseEntity> queryToOffer(@Param("query") LiftCaseQuery query);
    int queryToOfferCount(@Param("query") LiftCaseQuery query);
    /**
     * 查询专家待打卡
     * @param query
     * @return
     */
    List<LiftCaseEntity> queryToClock(@Param("query") LiftCaseQuery query);
    int queryToClockCount(@Param("query") LiftCaseQuery query);
    /**
     * 查询专家待确认
     * @param query
     * @return
     */
    List<LiftCaseEntity> queryChargeToSure(@Param("query") LiftCaseQuery query);
    int queryChargeToSureCount(@Param("query") LiftCaseQuery query);
    /**
     * 查询专家待归档
     * @param query
     * @return
     */
    List<LiftCaseEntity> queryToAchive(@Param("query") LiftCaseQuery query);
    int queryToAchiveCount(@Param("query") LiftCaseQuery query);
    /**
     * 专家工作台
     * @param query
     * @return
     */
    List<LiftCaseEntity> queryWorkbench(@Param("query") LiftCaseQuery query);
    int queryWorkbenchCount(@Param("query") LiftCaseQuery query);
    /**
     * 后台监控中心
     * @param query
     * @return
     */
    List<LiftCaseEntity> queryMonitoringCenter(@Param("query") LiftCaseQuery query);
    int queryMonitoringCenterCount(@Param("query") LiftCaseQuery query);



    List<LiftCaseEntity> queryPageByInvoiceId(@Param("id") Long invoiceId, @Param("page") LiftCaseQuery query);

    int countByInvoiceId(@Param("id") Long invoiceId);

    @Update("update lift_case set statuz=#{status} where id=#{caseId}")
    int updateStatusById(@Param("caseId") Long caseId, @Param("status") Integer status);

    @Update("update lift_case set statuz=2,travel_cost=#{travelCost},service_cost=#{serviceCost},total_cost=#{total} where id=#{id}")
    int toOffer(@Param("id") Long id, @Param("serviceCost") BigDecimal serviceCost,
        @Param("travelCost") BigDecimal travelCost, @Param("total") BigDecimal total);

    @Update("update lift_case set arrived_flag=2,before_repair=#{url} where id=#{id}")
    int userToClock(@Param("id") Long id, @Param("url") String url);

    @Update("update lift_case set arrived_flag=1 where id=#{id}")
    int userToClock1(@Param("id") Long id);
    
    @Select("SELECT ROUND(6378.138 * 2 * ASIN(SQRT(POW(SIN((#{lat} * PI() / 180 - lat * PI() / 180) / 2),2) + COS(#{lat} * PI() / 180) * COS(lat * PI() / 180) * POW(SIN((#{lng} * PI() / 180 - lng * PI() / 180) / 2),2))) * 1000) AS juli FROM lift_case WHERE id=#{id}")
    Long chargeToClock(@Param("id") Long id, @Param("lat") String lat, @Param("lng") String lng);

    @Update("update lift_case set accept_status=2,statuz=8,charger_id=NULL,customer_service_id=NULL,is_platform_specified=0 where id=#{id}")
    int refusedExperts(@Param("id") Long id);

    @Select("select session_id  from lift_case where customer_service_id = #{userId} and statuz not in (5,6)")
    List<String> findListByUserId(@Param("userId") String userId);

//    @Select("SELECT t.*,users.`name` AS NAME,users.avatar_url AS avatarUrl,brand.`name` AS brandName FROM lift_case AS t\r\n"
//        + "           LEFT JOIN user_info AS users ON t.create_user_id = users.user_id\r\n"
//        + "            LEFT JOIN lift_brand AS brand ON brand.id = t.brand_id\r\n" + "           \r\n"
//        + "            WHERE ((t.data_table=1 AND t.statuz IN (4,5,6)) OR (t.data_table=2 AND t.statuz IN (5,6)))\r\n"
//        + "           AND is_invoice=0\r\n"
//        + "       AND t.id NOT IN (SELECT data_id FROM lift_case_appeal WHERE statuz=4)\r\n"
//        + "            AND create_user_id=#{userId}")
    List<LiftCaseEntity> getAllCaseToInvoice(@Param("userId") Long userId);

    LiftCaseEntity getAllById(@Param("id") Long id);

    // 查询用户待确认诊单
    @Select("SELECT * FROM lift_case WHERE ((data_table=1 AND statuz=3) OR (data_table=2 AND statuz=4)) AND DATE_SUB(create_time, INTERVAL -1 DAY)<NOW()")
    List<LiftCaseEntity> getAllToSueLiftCase();

    @Select("SELECT * FROM lift_case WHERE (((data_table=1 AND statuz=1) OR (data_table=2 AND statuz=0)) AND (charger_id!=-2) AND accept_status=0) OR(((data_table=1 AND statuz=1) OR (data_table=2 AND statuz=0)) AND is_platform_specified=1 AND accept_status=0 ) AND create_time < DATE_FORMAT(ADDDATE(NOW(),INTERVAL -20 MINUTE),'%Y-%m-%d %H:%i:%s') ")
    List<LiftCaseEntity> getAllTimeOutCase();
    
    @Update("UPDATE lift_case SET charger_id=#{chargeId} WHERE id=#{id} ")
    void invitationExpert(@Param("id")Long id,@Param("chargeId")Long chargeId);
    
//    @Update("UPDATE lift_case SET charger_id=#{chargeId},customer_service_id=#{cumId} WHERE id=#{id} ")
    @Update("UPDATE lift_case SET customer_service_id=#{cumId} WHERE id=#{id} ")
    void updateCustomerServiceId(@Param("id")Long id,@Param("cumId")Long cumId);

    @Update("UPDATE lift_case SET color_grade=color_grade+1 WHERE id=#{id} ")
    void updateColorGarde(@Param("id")Long id);
    
    @Select("SELECT * FROM lift_case WHERE (((data_table=1 AND statuz=1) OR (data_table=2 AND statuz=0)) AND (charger_id!=-2) AND accept_status=0) OR (((data_table=1 AND statuz=1) OR (data_table=2 AND statuz=0)) AND is_platform_specified=1 AND accept_status=0)")
    List<LiftCaseEntity>getCaseIsToAccept();

    /**
     * 查询问诊或者出诊单数（全部状态的）
     * @return
     */
    @Select("select count(id) from lift_case where data_table = #{dataTable} and charger_id = #{userId}")
    int selectDataTableCount(@Param("dataTable")Integer dataTable,@Param("userId")Long userId);

    /**
     * 专家我的接单-
     * @param userId
     * @return
     */
//    @Select("select count(id) from lift_case where data_table = #{dataTable} and charger_id = #{userId} and statuz < #{statuz}")
//    int selectDealingCount(@Param("dataTable")Integer dataTable,@Param("userId")Long userId,@Param("statuz")Integer statuz);
    int selectDealingCount(@Param("userId")Long userId);

//    @Update("update lift_case set statuz=9 where id=#{id}")
    void cancelCase(@Param("id") Long id);
}
