package com.bonus.canteen.core.order.weight.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bonus.canteen.core.order.deprecated.vo.AppPageOrdListVO;
import com.bonus.canteen.core.order.weight.dto.BuffetWeighBind;
import com.bonus.canteen.core.order.weight.dto.QueryWeighBindDetailDTO;
import com.bonus.canteen.core.order.weight.dto.WeightBindQueryDTO;
import com.bonus.canteen.core.order.weight.model.MacWeighBind;
import com.bonus.canteen.core.order.weight.vo.AliMacWeightBindVO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.time.LocalDateTime;
import java.util.List;

@Mapper
public interface MacWeighBindMapper extends BaseMapper<MacWeighBind> {
   @Select({"SELECT * FROM mac_weigh_bind  where  bind_state =1   and crtime < #{localDateTime}"})
   List<MacWeighBind> getUnBindList(@Param("localDateTime") LocalDateTime localDateTime);

   @Select({"SELECT * FROM mac_weigh_bind  where  bind_state =1 and serial_num = #{serialNum}   and crtime > #{localDateTime} order by crtime "})
   List<MacWeighBind> getBindListBefore(@Param("localDateTime") LocalDateTime localDateTime, @Param("serialNum") String serialNum);

   @Select({"SELECT serial_num FROM mac_weigh_bind  where  bind_state =1 and cust_id=#{custId}"})
   List<String> getBindSerialList(@Param("custId") Long custId);

   @Select({"select  count(1) from mac_weigh_bind where cust_id=#{custId}  and bind_state=1"})
   Integer getBindNumber(@Param("custId") Long custId);

   List<MacWeighBind> selectMacWeightBindList(@Param("macWeighBind") MacWeighBind macWeighBind);

   List<BuffetWeighBind> queryMacWeightBindDetails(@Param("macWeighBinds") MacWeighBind macWeighBinds);

   @Update({"update mac_weigh_bind SET bind_state=2 WHERE mac_ord_id=#{macOrdId} "})
   boolean updateBindState(@Param("macOrdId") String macOrdId);

   @Update({"update mac_weigh_bind SET bind_state=2 ,cust_id =#{custId} ,cust_limit_id= #{custLimitId} WHERE mac_ord_id=#{macOrdId} "})
   boolean updateBindStateAndId(@Param("macOrdId") String macOrdId, @Param("plateSerialNum") String plateSerialNum, @Param("custId") Long custId, @Param("custLimitId") Integer custLimitId);

   @Update({"update mac_weigh_bind set if_redress=#{ifRedress} ,redress_remarks =#{redressRemarks} ,resress_picurl =#{resressPicurl} WHERE mac_ord_id=#{macOrdId} "})
   boolean updateRedressStateMarks(@Param("ifRedress") Integer ifRedress, @Param("redressRemarks") String redressRemarks, @Param("macOrdId") String macOrdId, @Param("resressPicurl") String resressPicurl);

   @Update({"update mac_weigh_bind set if_redress=#{ifRedress}  WHERE mac_ord_id=#{macOrdId} "})
   boolean updateRedressState(@Param("ifRedress") Integer ifRedress, @Param("macOrdId") String macOrdId);

   AliMacWeightBindVO getAlifTokenAndAliSignByMacOrdId(String macOrdId);

   Page<MacWeighBind> getWeightBindByPage(Page page, @Param("param") WeightBindQueryDTO param);

   List<AppPageOrdListVO> queryWeighBindDetail(@Param("openId") String openid, @Param("param") QueryWeighBindDetailDTO param, @Param("minTime") LocalDateTime minTime, @Param("maxTime") LocalDateTime maxTime);

   @Select({"select count(*) from mac_weigh_bind where crtime between #{startTime} and #{endTime}"})
   Integer countBindBetweenTimes(@Param("startTime") LocalDateTime startTime, @Param("endTime") LocalDateTime endTime);
}
