<?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.jianshui.system.mapper.CompanyserviceMapper">

    <resultMap type="com.jianshui.common.core.domain.entity.Companyservice" id="CompanyserviceResult">
        <result property="companyid" column="companyid"/>
        <result property="sellertax" column="sellertax"/>
        <result property="sellername" column="sellername"/>
        <result property="selleraddress" column="selleraddress"/>
        <result property="sellerphone" column="sellerphone"/>
        <result property="sellerbank" column="sellerbank"/>
        <result property="selleraccount" column="selleraccount"/>
        <result property="phone" column="phone"/>
        <result property="status" column="status"/>
        <result property="secret" column="secret"/>
        <result property="identity" column="identity"/>

        <result property="rebackurl" column="rebackurl"/>
        <result property="rebackType" column="reback_type"/>

        <result property="createTime" column="createtime"/>
        <result property="createUser" column="create_user"/>
        <result property="createDept" column="create_dept"/>
        <collection property="companyserviceDetailList" column="companyid" select="findDetails"></collection>
    </resultMap>

    <sql id="selectCompanyserviceVo">
        select companyid,
               sellertax,
               sellername,
               selleraddress,
               sellerphone,
               sellerbank,
               selleraccount,
               phone,
               status,
               secret, identity, rebackurl, reback_type, createtime, create_user, create_dept
        from companyservice
    </sql>

    <select id="findDetails" resultType="com.jianshui.common.core.domain.entity.CompanyserviceDetail">
        select id,
               company_id,
               is_default,
               clerk,
               checker,
               payee,
               fjh,
               terminal_number,
               golden_tax_disk_no,
               create_by,
               create_time,
               update_by,
               udpate_time,
               client_ip
        from companyservice_detail
        where company_id = #{companyid}
    </select>

    <select id="selectCompanyserviceList" parameterType="com.jianshui.common.core.domain.entity.Companyservice"
            resultMap="CompanyserviceResult">
        <include refid="selectCompanyserviceVo"/>
        <where>
            <if test="sellertax != null  and sellertax != ''">and sellertax = #{sellertax}</if>
            <if test="sellername != null  and sellername != ''">and sellername like concat('%', #{sellername}, '%')</if>
            <if test="phone != null  and phone != ''">and phone = #{phone}</if>
            <if test="rebackType != null  and rebackType != ''">and reback_type = #{rebackType}</if>
        </where>
    </select>

    <select id="selectCompanyserviceByCompanyid" parameterType="Long" resultMap="CompanyserviceResult">
        <include refid="selectCompanyserviceVo"/>
        where companyid = #{companyid}
    </select>

    <select id="selectFirstCompanyserviceBySellerTax" parameterType="String" resultMap="CompanyserviceResult">
        <include refid="selectCompanyserviceVo"/>
        where `sellertax` = #{sellertax} order by companyid asc limit 1
    </select>

    <select id="selectCompanyserviceByIdentity" parameterType="String" resultMap="CompanyserviceResult">
        <include refid="selectCompanyserviceVo"/>
        where `identity` = #{identity}
    </select>

    <select id="selectCompanyserviceByTaxcode" parameterType="String" resultMap="CompanyserviceResult">
        <include refid="selectCompanyserviceVo"/>
        where `sellertax` = #{taxcode} limit 1
    </select>

    <insert id="insertCompanyservice" parameterType="com.jianshui.common.core.domain.entity.Companyservice"
            useGeneratedKeys="true" keyProperty="companyid">
        insert into companyservice
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="sellertax != null">sellertax,</if>
            <if test="sellername != null">sellername,</if>
            <if test="selleraddress != null">selleraddress,</if>
            <if test="sellerphone != null">sellerphone,</if>
            <if test="sellerbank != null">sellerbank,</if>
            <if test="selleraccount != null">selleraccount,</if>
            <if test="phone != null">phone,</if>
            <if test="status != null">status,</if>
            <if test="secret != null">secret,</if>
            <if test="identity != null">identity,</if>

            <if test="rebackurl != null">rebackurl,</if>
            <if test="rebackType != null and rebackType != ''">reback_type,</if>

            <if test="createTime != null">createtime,</if>
            <if test="createUser != null">create_user,</if>
            <if test="createDept != null">create_dept,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="sellertax != null">#{sellertax},</if>
            <if test="sellername != null">#{sellername},</if>
            <if test="selleraddress != null">#{selleraddress},</if>
            <if test="sellerphone != null">#{sellerphone},</if>
            <if test="sellerbank != null">#{sellerbank},</if>
            <if test="selleraccount != null">#{selleraccount},</if>
            <if test="phone != null">#{phone},</if>
            <if test="status != null">#{status},</if>
            <if test="secret != null">#{secret},</if>
            <if test="identity != null">#{identity},</if>

            <if test="rebackurl != null">#{rebackurl},</if>
            <if test="rebackType != null and rebackType != ''">#{rebackType},</if>

            <if test="createTime != null">#{createTime},</if>
            <if test="createUser != null">#{createUser},</if>
            <if test="createDept != null">#{createDept},</if>
        </trim>
    </insert>

    <update id="updateCompanyservice" parameterType="com.jianshui.common.core.domain.entity.Companyservice">
        update companyservice
        <trim prefix="SET" suffixOverrides=",">
            <if test="sellertax != null">sellertax = #{sellertax},</if>
            <if test="sellername != null">sellername = #{sellername},</if>
            <if test="selleraddress != null">selleraddress = #{selleraddress},</if>
            <if test="sellerphone != null">sellerphone = #{sellerphone},</if>
            <if test="sellerbank != null">sellerbank = #{sellerbank},</if>
            <if test="selleraccount != null">selleraccount = #{selleraccount},</if>
            <if test="phone != null">phone = #{phone},</if>
            <if test="status != null">status = #{status},</if>
            <if test="secret != null">secret = #{secret},</if>
            <if test="identity != null">identity = #{identity},</if>


            <if test="rebackurl != null">rebackurl = #{rebackurl},</if>
            <if test="rebackType != null and rebackType != ''">reback_type = #{rebackType},</if>


            <if test="createTime != null">createtime = #{createTime},</if>
            <if test="createUser != null">create_user = #{createUser},</if>
            <if test="createDept != null">create_dept = #{createDept},</if>
        </trim>
        where companyid = #{companyid}
    </update>

    <delete id="deleteCompanyserviceByCompanyid" parameterType="Long">
        delete
        from companyservice
        where companyid = #{companyid}
    </delete>

    <delete id="deleteCompanyserviceByCompanyids" parameterType="String">
        delete from companyservice where companyid in
        <foreach item="companyid" collection="array" open="(" separator="," close=")">
            #{companyid}
        </foreach>
    </delete>
</mapper>
