<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.ruoyi.platform.mapper.ServiceDao">
    <select id="countService" resultType="java.lang.Long">
        select count(1) from service
        <where>
            state = 1
            <if test="service.serviceName != null and service.serviceName != ''">
                and service_name like concat('%', #{service.serviceName}, '%')
            </if>
            <if test="service.serviceType != null and service.serviceType != ''">
                and service_type = #{service.serviceType}
            </if>
        </where>
    </select>

    <select id="queryByPageService" resultType="com.ruoyi.platform.domain.Service">
        select a.*,count(b.id) as version_count from service a
        left join (select * from service_version where state = 1) b on a.id = b.service_id
        <where>
            a.state = 1
            <if test="service.serviceName != null and service.serviceName != ''">
                and a.service_name like concat('%', #{service.serviceName}, '%')
            </if>
            <if test="service.serviceType != null and service.serviceType != ''">
                and a.service_type = #{service.serviceType}
            </if>
        </where>
        group by a.id
        order by a.update_time DESC
        limit #{pageable.offset}, #{pageable.pageSize}
    </select>

    <select id="countServiceVersion" resultType="java.lang.Long">
        select count(1) from
        ( <include refid="queryServiceVersion"></include> ) t
    </select>

    <select id="queryByPageServiceVersion" resultType="com.ruoyi.platform.domain.ServiceVersion">
        <include refid="queryServiceVersion"></include>
        limit #{pageable.offset}, #{pageable.pageSize}
    </select>

    <sql id="queryServiceVersion">
        select * from service_version
        <where>
            state = 1
            <if test="serviceVersion.version != null and serviceVersion.version !=''">
                and version like concat('%', #{serviceVersion.version}, '%')
            </if>
            <if test="serviceVersion.serviceId != null and serviceVersion.serviceId !=''">
                and service_id = #{serviceVersion.serviceId}
            </if>
        </where>
        order by update_time DESC
    </sql>

    <select id="getServiceById" resultType="com.ruoyi.platform.domain.Service">
        select a.*,count(b.id) as version_count
        from service a
        left join (select * from service_version where state = 1) b on a.id = b.service_id
        where a.id = #{id}
    </select>

    <select id="getServiceVersionById" resultType="com.ruoyi.platform.domain.ServiceVersion">
        select *
        from service_version
        where id = #{id}
    </select>

    <select id="getServiceVersionList" resultType="com.ruoyi.platform.domain.ServiceVersion">
        select *
        from service_version
        where service_id = #{serviceId}
    </select>

    <select id="getServiceByName" resultType="com.ruoyi.platform.domain.Service">
        select * from service where service_name = #{serviceName} and state = 1
    </select>

    <select id="getSvByVersion" resultType="com.ruoyi.platform.domain.ServiceVersion">
        select *
        from service_version
        where service_id = #{serviceId} and version = #{version} and state = 1
    </select>

    <insert id="insertService" keyProperty="id" useGeneratedKeys="true">
        insert into service(service_name, service_type, description, create_by, update_by)
        values (#{service.serviceName}, #{service.serviceType}, #{service.description}, #{service.createBy}, #{service.updateBy})
    </insert>

    <insert id="insertServiceVersion" keyProperty="id" useGeneratedKeys="true">
        insert into service_version(service_id, version, model, description, image, resource, replicas, mount_path, env_variables,
                                    code_config, command, create_by, update_by)
        values (#{serviceVersion.serviceId}, #{serviceVersion.version}, #{serviceVersion.model}, #{serviceVersion.description}, #{serviceVersion.image},
                #{serviceVersion.resource}, #{serviceVersion.replicas}, #{serviceVersion.mountPath}, #{serviceVersion.envVariables},
                #{serviceVersion.codeConfig}, #{serviceVersion.command}, #{serviceVersion.createBy}, #{serviceVersion.updateBy})
    </insert>

    <update id="updateService">
        update service
        <set>
            <if test="service.serviceName != null and service.serviceName !=''">
                service_name = #{service.serviceName},
            </if>
            <if test="service.serviceType != null and service.serviceType !=''">
                service_type = #{service.serviceType},
            </if>
            <if test="service.description != null and service.description !=''">
                description = #{service.description},
            </if>
            <if test="service.state != null">
                state = #{service.state},
            </if>
        </set>
        where id = #{service.id}
    </update>

    <update id="updateServiceVersion">
        update service_version
        <set>
            <if test="serviceVersion.description != null and serviceVersion.description !=''">
                description = #{serviceVersion.description},
            </if>
            <if test="serviceVersion.model != null and serviceVersion.model !=''">
                model = #{serviceVersion.model},
            </if>
            <if test="serviceVersion.image != null and serviceVersion.image !=''">
                image = #{serviceVersion.image},
            </if>
            <if test="serviceVersion.resource != null and serviceVersion.resource !=''">
                resource = #{serviceVersion.resource},
            </if>
            <if test="serviceVersion.replicas != null">
                replicas = #{serviceVersion.replicas},
            </if>
            <if test="serviceVersion.mountPath != null and serviceVersion.mountPath !=''">
                mount_path = #{serviceVersion.mountPath},
            </if>
            <if test="serviceVersion.envVariables != null and serviceVersion.envVariables !=''">
                env_variables = #{serviceVersion.envVariables},
            </if>
            <if test="serviceVersion.codeConfig != null and serviceVersion.codeConfig !=''">
                code_config = #{serviceVersion.codeConfig},
            </if>
            <if test="serviceVersion.command != null and serviceVersion.command !=''">
                command = #{serviceVersion.command},
            </if>
            <if test="serviceVersion.url != null and serviceVersion.url !=''">
                url = #{serviceVersion.url},
            </if>
            <if test="serviceVersion.state != null">
                state = #{serviceVersion.state},
            </if>
            <if test="serviceVersion.runState != null and serviceVersion.runState !=''">
                run_state = #{serviceVersion.runState},
            </if>
            <if test="serviceVersion.deploymentName != null and serviceVersion.deploymentName !=''">
                deployment_name = #{serviceVersion.deploymentName},
            </if>
            <if test="serviceVersion.svcName != null and serviceVersion.svcName !=''">
                svc_name = #{serviceVersion.svcName},
            </if>
        </set>
        where id = #{serviceVersion.id}
    </update>

    <update id="updateRunState">
        update service_version
        set run_state = #{runState}
        where deployment_name = #{deploymentName}
    </update>
</mapper>