package org.dromara.app.mapper;

import org.dromara.app.domain.AppDevice;
import org.dromara.app.domain.vo.AppDeviceVo;
import org.dromara.common.mybatis.annotation.DataColumn;
import org.dromara.common.mybatis.annotation.DataPermission;
import org.dromara.common.mybatis.core.mapper.BaseMapperPlus;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Update;

/**
 * 设备信息
Mapper接口
 *
 * @author Lion Li
 * @date 2026-04-25
 */
public interface AppDeviceMapper extends BaseMapperPlus<AppDevice, AppDeviceVo> {

    @Update("""
        <script>
        update app_device
        <set>
            user_id = #{userId},
            bind_token_hash = null,
            <if test="deviceName != null and deviceName != ''">
                device_name = #{deviceName},
            </if>
            <if test="status != null and status != ''">
                status = #{status},
            </if>
        </set>
        where device_no = #{deviceNo}
          and (user_id is null or user_id = #{userId})
        </script>
        """)
    int bindIfAvailable(@Param("deviceNo") String deviceNo,
                        @Param("userId") Long userId,
                        @Param("deviceName") String deviceName,
                        @Param("status") String status);

    @Update("""
        update app_device
        set work_status = #{workStatus}
        where device_no = #{deviceNo}
          and user_id = #{userId}
        """)
    int updateWorkStatusByUser(@Param("deviceNo") String deviceNo,
                               @Param("userId") Long userId,
                               @Param("workStatus") String workStatus);

}
