#namespace("companyMember")
    #sql("findUserExist")
        select * from company_member where deleted=0 and company_id=(
            select id from company where name=? and deleted=0
        ) and uid=(
            select id from user where id_car_no=? and deleted=0
        )
    #end

    #sql("findUserExistByCompanyAndName")
        SELECT
            cm.company_id,cm.user_id,c.`name`,cm.uid,u.`name` AS user_name
        FROM
            company_member cm
                LEFT JOIN `user` u
                          ON cm.uid = u.id AND u.deleted = 0
                LEFT JOIN company c
                          ON cm.company_id = c.id AND c.deleted = 0
        WHERE
            cm.deleted = 0
            AND cm.company_id = #para(companyId)
            AND u.name = #para(userName)
            AND cm.user_id IS NULL
    #end

    #sql("findQyUserByCompanyAndName")
        SELECT * FROM company_member
            WHERE deleted = 0 AND company_id = #para(cid) AND uid IN (
                SELECT id FROM `user` WHERE deleted = 0 AND tag = 2 AND `name` = #para(userName) AND id_car_no IS NULL
            )
    #end
#end


#namespace("company")
    #sql("findUserCompany")
        select * from company where deleted=0 and id in (
            select company_id from company_member where uid=? and deleted=0
        )
    #end
#end


#namespace("role")
    #sql("findNameByUid")
        SELECT
            r.name
        FROM
            user_role ur
            LEFT JOIN role r ON ur.rid = r.id AND r.deleted = 0
        WHERE ur.deleted = 0 AND ur.uid = #para(uid)
    #end
#end


#namespace("tradRecord")
    #sql("findAll")
        select * from trad_record where deleted = 0
            #for(x : kv)
                #("and") #(x.key) #para(x.value)
              #end
        order by create_time desc
    #end
#end


#namespace("tag")
    #sql("findTagByType")
        select *
        from tag
        where deleted = 0
                #for(x : kv)
                    #("and") #(x.key) #para(x.value)
                #end
    #end
#end


#namespace("wallet")
    #sql("findWalletByDeviceCompany")
        SELECT w.*
        FROM wallet w
        INNER JOIN (
            SELECT o.uid
            FROM `order` o
            LEFT JOIN device d ON o.device_id = d.id AND d.deleted = 0
            WHERE o.deleted = 0
            AND d.cid = #para(cid)
            GROUP BY o.uid
            ) u ON w.uid = u.uid
        WHERE w.deleted = 0 AND w.org_code is null
    #end

    #sql("findWalletByPay")
        SELECT
            w.*,u.code,w.total_money-w.frozen_money-IFNULL(o.frozen_money,0) AS available_money
        FROM
            wallet w
                INNER JOIN user u ON w.uid = u.id AND u.deleted = 0
                LEFT JOIN `order` o ON w.uid = o.uid AND o.deleted = 0 AND o.`status` = 0 AND (o.way = 3 OR o.way = 0)
        WHERE
                w.deleted = 0
          AND w.uid IN ( SELECT id FROM `user` WHERE deleted = 0 AND tag != 0 AND tag != 2 AND wx_name = #para(wxName) )
          AND w.org_code IS NULL
          #if(code != null)
            and u.code = #para(code)
          #end
        ORDER BY available_money DESC
    #end
#end


#namespace("rechargeOrder")
    #sql("findPage")
        select *, money-refund as available_refund from recharge_order where (status = 2 or status = 5)
            #for(x : kv)
                #("and") #(x.key) #para(x.value)
            #end
        order by available_refund desc, create_time desc
    #end
#end


#namespace("extractCash")
    #sql("findPage")
        SELECT c.`name`,e.* FROM extract_cash e JOIN company c ON e.org_code = c.org_code WHERE e.deleted = 0
            #if(orgCode != null)
               and e.org_code = #para(orgCode)
            #end
            #if(status != null)
               and e.`status` = #para(status)
            #end
        order by e.create_time desc
    #end
#end


#namespace("menu")
    #sql("queryChildren")
        SELECT * FROM menu WHERE deleted = 0 AND ancestors like concat(#para(ancestors),'%')
            #if(group != null)
               and `group` = #para(group)
            #end
            #if(type != null)
               and `type` = #para(type)
            #end
            #if(pid != null)
               and pid = #para(pid)
            #end
    #end


    #sql("queryByUser")
        SELECT * FROM menu WHERE deleted = 0 AND id IN (
            SELECT mid FROM role_menu WHERE deleted = 0 AND rid IN(
                SELECT rid FROM user_role WHERE deleted = 0 AND uid = #para(uid)
                )
            )
        #if(group != null)
           and `group` = #para(group)
        #end
        #if(type != null)
           and `type` = #para(type)
        #end
    #end


    #sql("queryByRole")
        SELECT * FROM menu WHERE deleted = 0 AND id IN (SELECT mid FROM role_menu WHERE deleted = 0 AND rid = #para(rid))
        #if(group != null)
           and `group` = #para(group)
        #end
        #if(type != null)
           and `type` = #para(type)
        #end
    #end


    #sql("findAuthCodeByRoleIds")
        select auth_code from menu
            where deleted = 0 and `type` = 1 and id IN (
                SELECT mid from role_menu WHERE deleted = 0
                    #if(roleIdList != null)
                        AND rid IN (
                            #for(k : roleIdList)
                                #(for.index > 0 ? ", " : "")#(k)
                            #end
                        )
                    #end
                )
    #end

#end



#namespace("eleRecord")
    #sql("findPage")
        SELECT
            e.id,
            e.money,
            e.device_id,
            e.uid,
            e.create_time,
            e.create_user,
            e.model,
            d.type
        FROM
            ele_record e
                LEFT JOIN device d ON e.device_id = d.id
        WHERE
                e.deleted = 0
            #if(uid != null)
               and e.uid = #para(uid)
            #end
            #if(deviceId != null)
               and e.device_id = #para(deviceId)
            #end
            #if(model != null)
               and e.model = #para(model)
            #end
            #if(ascSorted)
                order by e.create_time asc
            #end
            #if(!ascSorted)
                order by e.create_time desc
            #end
    #end
#end



#namespace("refund")
    #sql("findPage")
        SELECT * FROM refund WHERE deleted = 0

            #if(queryByUid)
                #if(uid != null)
                   and uid = #para(uid)
                #end
            #end

            #if(status != null)
               and status = #para(status)
            #end
        order by create_time desc
    #end
#end


#namespace("eleMoney")
    #sql("findPage")
        SELECT
            d.id,d. model,d.model_simple,d.type,d.address,d.latitude,d.longitude,d.company_create,d.line,d.`status`,d.status_line,d.create_time,
            dp.price,
            IFNULL(e.total_money,0) AS total_money,IFNULL(e.residue_money,0) AS residue_money,IFNULL(e.use_money,0) AS use_money,IFNULL(e.extract_money,0) AS extract_money
        FROM
            device d
                LEFT JOIN ele_money e ON d.id = e.device_id AND e.deleted = 0
                LEFT JOIN device_price dp ON d.id = dp.device_id AND dp.deleted = 0
        WHERE
                d.deleted = 0
          AND d.scene_tag = 1
        ORDER BY d.create_time DESC
    #end
#end


#namespace("extractRecord")
    #sql("findPage")
        SELECT
            er.*,d.model,d.model_simple
        FROM
            extract_record er
                LEFT JOIN device d ON er.device_id = d.id AND d.deleted = 0
        WHERE
                er.deleted = 0
          and er.org_code = #para(orgCode)
            #if(beginTime != null)
               and er.create_time >= #para(beginTime)
            #end
            #if(endTime != null)
               and er.create_time <= #para(endTime)
            #end
            #if(ascSorted)
                order by er.create_time asc
            #end
            #if(!ascSorted)
                order by er.create_time desc
            #end
    #end
#end


#namespace("deviceOperate")
    #sql("findPageAll")
        select * from device_operate where deleted = 0
            #if(beginTime != null && endTime != null)
               AND create_time between #para(beginTime) and #para(endTime)
            #end
            #if(beginTime != null && endTime == null)
               AND create_time >= #para(beginTime)
            #end
            #if(beginTime == null && endTime != null)
               AND create_time <= #para(endTime)
            #end
            #if(model != null)
               and model = #para(model)
            #end
            #if(source != null)
               and source = #para(source)
            #end
            #if(sorted != null && sorted == 0)
                order by create_time asc
            #end
            #if(sorted != null && sorted == 1)
                order by create_time desc
            #end
    #end
#end


#namespace("deviceState")
    #sql("findPageAll")
        select * from device_state
            where deleted = 0
            AND model IS NOT NULL
            AND type IS NOT NULL
            #for(x : kv)
                #("and") #(x.key) #para(x.value)
            #end
            #for(x : kv2)
                #if(x.key != null && x.value != null)
                    and create_time between #para(x.key) and #para(x.value)
                #end
            #end
        order by create_time desc
    #end
#end




#namespace("device")
    #sql("findPageAllDevice")
        select d.*, c.name as company_name, dp.price, t.name as tag_name
        from device d
                 left join device_price dp on d.id = dp.device_id and dp.deleted = 0
                 left join company c on d.cid = c.id and c.deleted = 0
                 left join tag t on d.tag_id = t.id and t.deleted = 0
        where d.deleted = 0
                #for(x : kv)
                    #("and") #(x.key) #para(x.value)
                #end
                #for(x : kv2)
                    #if(x.value != null && x.value != "")
                        and (
                        c.name like concat('%',#para(x.value),'%') or
                        d.address like concat('%',#para(x.value),'%') or
                        d.name like concat('%',#para(x.value),'%') or
                        d.model_simple like concat('%',#para(x.value),'%')
                        )
                    #end
                #end
                #for(x : kv3)
                    #if(x.value != null && x.value != "")
                        and d.tag_id in (
                            #for(k : x.value)
                                #(for.index > 0 ? ", " : "")#(k)
                            #end
                        )
                    #end
                #end
        order by d.create_time desc
    #end



    #sql("findPageAllDeviceDistance")
        SELECT d.*,
               c.name AS company_name,
               dp.price,
               t.name as tag_name
        FROM (
                 SELECT *,
                        IFNULL(ROUND(ST_Distance_Sphere(point(longitude, latitude), point(#para(lon), #para(lat))) / 1000, 2),
                               0) AS distance
                 FROM device
                 WHERE deleted = 0
             ) AS d
                 LEFT JOIN device_price dp ON d.id = dp.device_id AND dp.deleted = 0
                 LEFT JOIN company c ON d.cid = c.id AND c.deleted = 0
                 left join tag t on d.tag_id = t.id and t.deleted = 0
        WHERE 1 = 1
                #if(distance != null)
                    and d.distance <= #para(distance)
                #end
                #for(x : kv)
                    #("and") #(x.key) #para(x.value)
                #end
                #for(x : kv2)
                    #if(x.value != null && x.value != "")
                        and (
                        c.name like concat('%',#para(x.value),'%') or
                        d.address like concat('%',#para(x.value),'%') or
                        d.name like concat('%',#para(x.value),'%') or
                        d.model_simple like concat('%',#para(x.value),'%')
                        )
                    #end
                #end
                #for(x : kv3)
                    #if(x.value != null && x.value != "")
                        and d.tag_id in (
                            #for(k : x.value)
                                #(for.index > 0 ? ", " : "")#(k)
                            #end
                        )
                    #end
                #end
        order by
            #if(distance != null)
            d.distance asc,
            #end
            d.create_time desc
    #end



    #sql("findData")
        select vo.* from(
                    SELECT
                        d.id,d.name,d.model,d.type,d.address,d.tag_id,t.name AS tag_name,d.status,d.status_line,d.longitude,d.latitude,o3.id AS not_close_order_id,o3.way,
                        IFNULL(COUNT(DISTINCT os1.order_id),0) AS orders_created,
                        IFNULL(COUNT(DISTINCT os2.order_id),0) AS orders_completed,
                        IFNULL(COUNT(DISTINCT os1.order_id)-COUNT(DISTINCT os2.order_id),0) AS orders_uncompleted,
                        (SELECT IFNULL(ROUND(MAX(ds.power) - MIN(ds.power),2),0)
                         FROM device_state ds
                         WHERE ds.deleted = 0 AND ds.model = d.model AND ds.create_time between #para(beginTime) and #para(endTime) ) AS power,
                        ROUND(IFNULL(SUM(tr.trad_money),0), 2) AS order_income,
                        ROUND(IFNULL(SUM(tr.trad_money),0) + (
                            SELECT IFNULL(ROUND(SUM(CASE WHEN o3.way = 1 THEN kilowatt * price
                                                         WHEN o3.way = 2 THEN money
                                                         WHEN o3.way = 0 THEN
                                                             (SELECT IFNULL(AVG(active_power), 6) * o3.hours * o3.price / 1000
                                                              FROM device_state
                                                              WHERE deleted = 0 AND voltage > 0 AND operate = 'readAll' AND model = d.model
                                                                AND create_time > o3.create_time
                                                              ORDER BY create_time DESC
                                                             LIMIT 5)
                                     ELSE 0 END), 2), 0)
                            FROM `order` o3
                            WHERE d.id = o3.device_id AND o3.deleted = 0 AND o3.status = 0 AND o3.create_time between #para(beginTime) and #para(endTime)
                        ), 2) AS all_order_income,
                        (
                            SELECT IFNULL(ROUND(SUM(CASE WHEN o3.way = 1 THEN kilowatt * price
                                                         WHEN o3.way = 2 THEN money
                                                         WHEN o3.way = 0 THEN
                                                             (SELECT IFNULL(AVG(active_power), 6) * o3.hours * o3.price / 1000
                                                              FROM device_state
                                                              WHERE deleted = 0 AND voltage > 0 AND operate = 'readAll' AND model = d.model
                                                                AND create_time > o3.create_time
                                                              ORDER BY create_time DESC
                                                             LIMIT 5)
                                     ELSE 0 END), 2), 0)
                            FROM `order` o3
                            WHERE d.id = o3.device_id AND o3.deleted = 0 AND o3.status = 0 AND o3.create_time between #para(beginTime) and #para(endTime)
                        ) AS expect_order_income
                    FROM
                        device d
                            LEFT JOIN tag t ON d.tag_id = t.id AND t.deleted = 0
                            LEFT JOIN
                        `order` o1 ON d.id = o1.device_id AND o1.deleted = 0 AND o1.create_time between #para(beginTime) and #para(endTime)
                            LEFT JOIN
                        `order` o2 ON d.id = o2.device_id AND o2.deleted = 0 AND o2.create_time between #para(beginTime) and #para(endTime)
                            LEFT JOIN
                        order_status os1 ON o1.id = os1.order_id AND os1.deleted = 0 AND os1.status = 0 AND os1.create_time between #para(beginTime) and #para(endTime)
                            LEFT JOIN
                        order_status os2 ON o2.id = os2.order_id AND os2.deleted = 0 AND os2.status = 1 AND os2.create_time between #para(beginTime) and #para(endTime)
                            LEFT JOIN
                        trad_record tr ON o2.`code` = CAST(SUBSTRING_INDEX(SUBSTRING_INDEX(tr.trad_way, '#', -2), '扣减', 1) AS UNSIGNED) AND tr.deleted = 0 AND tr.trad_type = 1 AND tr.business = 0 AND tr.trad_channel = 4
                            LEFT JOIN `order` o3 ON d.id = o3.device_id AND o3.deleted = 0 AND o3.status = 0 AND o3.create_time between #para(beginTime) and #para(endTime)
                    WHERE
                            d.deleted = 0 AND d.company_create = 1
                        #if(model != null)
       and d.model = #para(model)
        #end
        #if(cid != null)
           and d.cid = #para(cid)
        #end
        #if(tagIds != null)
            and d.tag_id in (
                #for(k : tagIds)
                    #(for.index > 0 ? ", " : "")#(k)
                #end
            )
        #end
                        GROUP BY
                            d.id, d.model
                    ) vo
    #end




    #sql("findTotalDataByCid")
        SELECT
            IFNULL(COUNT(*),0) AS total_devices,
            IFNULL(SUM(orders_created),0) AS total_orders_created,
            IFNULL(SUM(orders_completed),0) AS total_orders_completed,
            IFNULL(SUM(orders_uncompleted),0) AS total_orders_uncompleted,
            IFNULL(ROUND(SUM(power),2),0) AS total_power,
            IFNULL(ROUND(SUM(order_income),2),0) AS total_order_income,
            IFNULL(ROUND(SUM(all_order_income),2),0) AS total_all_order_income,
            IFNULL(ROUND(SUM(expect_order_income),2),0) AS total_expect_order_income
        FROM (
                 SELECT
                     d.id,d.name,d.model,d.type,d.address,d.tag_id,t.name AS tag_name,d.status,d.status_line,d.longitude,d.latitude,o3.id AS not_close_order_id,o3.way,
                     COUNT(DISTINCT os1.order_id) AS orders_created,
                     COUNT(DISTINCT os2.order_id) AS orders_completed,
                     COUNT(DISTINCT os1.order_id)-COUNT(DISTINCT os2.order_id) AS orders_uncompleted,
                     (SELECT IFNULL(ROUND(MAX(ds.power) - MIN(ds.power),2),0)
                      FROM device_state ds
                      WHERE ds.deleted = 0 AND ds.model = d.model AND ds.create_time between #para(beginTime) and #para(endTime) ) AS power,
                     ROUND(IFNULL(SUM(tr.trad_money),0), 2) AS order_income,
                     ROUND(IFNULL(SUM(tr.trad_money),0) + (
                         SELECT IFNULL(ROUND(SUM(CASE WHEN o3.way = 1 THEN kilowatt * price
                                                      WHEN o3.way = 2 THEN money
                                                      WHEN o3.way = 0 THEN
                                                          (SELECT IFNULL(AVG(active_power), 6) * o3.hours * o3.price / 1000
                                                           FROM device_state
                                                           WHERE deleted = 0 AND voltage > 0 AND operate = 'readAll' AND model = d.model
                                                             AND create_time > o3.create_time
                                                           ORDER BY create_time DESC
                                                          LIMIT 5)
                                                 ELSE 0 END), 2), 0)
                         FROM `order` o3
                         WHERE d.id = o3.device_id AND o3.deleted = 0 AND o3.status = 0 AND o3.create_time between #para(beginTime) and #para(endTime)
                     ), 2) AS all_order_income,
                     (
                         SELECT IFNULL(ROUND(SUM(CASE WHEN o3.way = 1 THEN kilowatt * price
                                                      WHEN o3.way = 2 THEN money
                                                      WHEN o3.way = 0 THEN
                                                          (SELECT IFNULL(AVG(active_power), 6) * o3.hours * o3.price / 1000
                                                           FROM device_state
                                                           WHERE deleted = 0 AND voltage > 0 AND operate = 'readAll' AND model = d.model
                                                             AND create_time > o3.create_time
                                                           ORDER BY create_time DESC
                                                          LIMIT 5)
                                                 ELSE 0 END), 2), 0)
                         FROM `order` o3
                         WHERE d.id = o3.device_id AND o3.deleted = 0 AND o3.status = 0 AND o3.create_time between #para(beginTime) and #para(endTime)
                     ) AS expect_order_income
                 FROM
                     device d
                         LEFT JOIN tag t ON d.tag_id = t.id AND t.deleted = 0
                         LEFT JOIN
                     `order` o1 ON d.id = o1.device_id AND o1.deleted = 0 AND o1.create_time between #para(beginTime) and #para(endTime)
                         LEFT JOIN
                     `order` o2 ON d.id = o2.device_id AND o2.deleted = 0 AND o2.create_time between #para(beginTime) and #para(endTime)
                         LEFT JOIN
                     order_status os1 ON o1.id = os1.order_id AND os1.deleted = 0 AND os1.status = 0 AND os1.create_time between #para(beginTime) and #para(endTime)
                         LEFT JOIN
                     order_status os2 ON o2.id = os2.order_id AND os2.deleted = 0 AND os2.status = 1 AND os2.create_time between #para(beginTime) and #para(endTime)
                         LEFT JOIN
                     trad_record tr ON o2.`code` = CAST(SUBSTRING_INDEX(SUBSTRING_INDEX(tr.trad_way, '#', -2), '扣减', 1) AS UNSIGNED) AND tr.deleted = 0 AND tr.trad_type = 1 AND tr.business = 0 AND tr.trad_channel = 4
                         LEFT JOIN `order` o3 ON d.id = o3.device_id AND o3.deleted = 0 AND o3.status = 0 AND o3.create_time between #para(beginTime) and #para(endTime)
                 WHERE
                         d.deleted = 0 AND d.company_create = 1
                     #if(cid != null)
                   and d.cid = #para(cid)
                #end
                 GROUP BY
                     d.id, d.model
             ) AS summary;
    #end



    #sql("update")
        update device
        set
            modify_time = #para(modifyTime),
            modify_user = #para(modifyUser)

            #if(address != null)
                    ,address = #para(address)
                #end
                #if(latitude != null)
                    ,latitude = #para(latitude)
                #end
                #if(longitude != null)
                    ,longitude = #para(longitude)
                #end
                #if(description != null)
                    ,description = #para(description)
                #end
                #if(deviceProp != null)
                    ,device_prop = #para(deviceProp)
                #end
                #if(line != null)
                    ,line = #para(line)
                #end
        where id = #para(id)
    #end



    #sql("findDeviceParamCount")
        SELECT
            (SELECT COUNT(*) FROM device WHERE cid = #para(cid) AND deleted = 0) AS total_devices,
            (SELECT COUNT(*) FROM device WHERE cid = #para(cid) AND deleted = 0 AND line = 1) AS line_devices,
            (SELECT COUNT(*) FROM device WHERE cid = #para(cid) AND deleted = 0 AND status_line = 0) AS offline_count,
            (SELECT COUNT(*) FROM device WHERE cid = #para(cid) AND deleted = 0 AND status_line = 1) AS line_count,
            (SELECT COUNT(*) FROM device d JOIN device_price dp ON d.id = dp.device_id AND dp.deleted = 0 WHERE d.cid = #para(cid) AND d.deleted = 0 AND dp.price IS NOT NULL) AS price_devices;
    #end




    #sql("findPageAllBasicParam")
        select d.*, c.name as company_name, dp.price, t.name as tag_name
        from device d
                 left join device_price dp on d.id = dp.device_id and dp.deleted = 0
                 left join company c on d.cid = c.id and c.deleted = 0
                 left join tag t on d.tag_id = t.id and t.deleted = 0
        where d.deleted=0
            #if(cid != null)
              and d.cid = #para(cid)
            #end
            #if(companyCreate != null)
              and d.company_create = #para(companyCreate)
            #end
            #if(line != null)
              and d.line = #para(line)
            #end
            #if(status != null)
              and d.status = #para(status)
            #end
            #if(deviceProp != null)
              and d.device_prop = #para(deviceProp)
            #end
            #if(sceneTag != null)
              and d.scene_tag = #para(sceneTag)
            #end
            #if(startPrice != null)
              and dp.price >= #para(startPrice)
            #end
            #if(endPrice != null)
              and dp.price <= #para(endPrice)
            #end

            #if(name != null)
                and (
                c.name like concat('%',#para(name),'%') or
                d.address like concat('%',#para(name),'%') or
                d.name like concat('%',#para(name),'%') or
                d.model_simple like concat('%',#para(name),'%')
                )
            #end
            #if(tagIds != null)
                and d.tag_id in (
                    #for(k : tagIds)
                        #(for.index > 0 ? ", " : "")#(k)
                    #end
                )
            #end

        order by d.create_time desc
    #end




    #sql("findPageAllDistanceParam")
        SELECT
            d.*,
            c.name AS company_name,
            dp.price,
            t.name AS tag_name
        FROM
            (
                SELECT
                    *,
                    IFNULL(ROUND(ST_Distance_Sphere(point(longitude, latitude), point(#para(lon), #para(lat))) / 1000,2),0) AS distance
                FROM
                    device
                WHERE
                        deleted = 0
            ) AS d
                LEFT JOIN device_price dp ON d.id = dp.device_id AND dp.deleted = 0
                LEFT JOIN company c ON d.cid = c.id AND c.deleted = 0
                left join tag t on d.tag_id = t.id and t.deleted=0
        WHERE
                1 = 1
            #if(distance != null)
                    and d.distance <= #para(distance)
            #end
            #if(companyCreate != null)
              and d.company_create = #para(companyCreate)
            #end
            #if(line != null)
              and d.line = #para(line)
            #end
            #if(status != null)
              and d.status = #para(status)
            #end
            #if(deviceProp != null)
              and d.device_prop = #para(deviceProp)
            #end
            #if(sceneTag != null)
              and d.scene_tag = #para(sceneTag)
            #end

            #if(name != null)
                and (
                c.name like concat('%',#para(name),'%') or
                d.address like concat('%',#para(name),'%') or
                d.name like concat('%',#para(name),'%') or
                d.model_simple like concat('%',#para(name),'%')
                )
            #end
            #if(tagIds != null)
                and d.tag_id in (
                    #for(k : tagIds)
                        #(for.index > 0 ? ", " : "")#(k)
                    #end
                )
            #end
        order by
            #if(distance != null)
            d.distance asc,
            #end
            d.create_time desc
    #end






#end






#namespace("order")
    #sql("findUserPageOrder")
        SELECT
            o.* , d.model_simple, d.name
        FROM
            `order` o
                LEFT JOIN device d ON o.model = d.model AND d.deleted = 0 AND d.company_create = 1
        WHERE o.deleted = 0
            #if(id != null)
               and o.id = #para(id)
            #end
            #if(status != null)
               and o.status = #para(status)
            #end
            #if(uid != null)
               and o.uid = #para(uid)
            #end
            #if(beginTime != null)
               and o.create_time >= #para(beginTime)
            #end
            #if(endTime != null)
               and o.create_time <= #para(endTime)
            #end
            #if(ascSorted)
                order by o.create_time asc
            #end
            #if(!ascSorted)
                order by o.create_time desc
            #end
    #end


    #sql("findOrderByCompany")
        SELECT
            o.id,
            o.`code`,
            o.`status`,
            o. price,
            o.use_power,
            o.create_time,
            d.id AS device_id,
            d.`name`,
            d.model,
            d.model_simple
        FROM
            `order` o
                LEFT JOIN device d ON o.device_id = d.id AND d.deleted = 0
        WHERE
                o.deleted = 0 AND d.cid = #para(cid)
            #if(name != null)
               and (o.code like concat('%',#para(name),'%') or d.`name` like concat('%',#para(name),'%') or d.model_simple like concat('%',#para(name),'%'))
            #end
            #if(beginTime != null)
               and o.create_time >= #para(beginTime)
            #end
            #if(endTime != null)
               and o.create_time <= #para(endTime)
            #end
    #end



    #sql("findDataForOrder")
        SELECT
            o.id,o.`code`,o.`status`,o.price,o.device_id,d.`name`,d.model,d.model_simple,d.type,o.create_time,o.real_end_time,d.tag_id,t.`name` AS tag_name
        FROM
            `order` o
                LEFT JOIN device d ON o.device_id = d.id AND d.deleted = 0
                LEFT JOIN tag t ON d.tag_id = t.id AND t.deleted = 0
        WHERE o.deleted = 0

            #if(cid != null)
                   and d.cid = #para(cid)
                #end

                #if(tagIds != null)
                    and d.tag_id in (
                        #for(k : tagIds)
                            #(for.index > 0 ? ", " : "")#(k)
                        #end
                    )
                #end

                #if(name != null)
                   and (
                        o.`code` like concat('%',#para(name),'%') or d.`name` like concat('%',#para(name),'%') or d.model_simple like concat('%',#para(name),'%')
                   )
                #end
                #if(model != null)
                   and d.model like concat('%',#para(name),'%')
                #end

                #if(beginTime != null)
                   and o.create_time >= #para(beginTime)
                #end
                #if(endTime != null)
                   and o.create_time <= #para(endTime)
                #end

        order by o.create_time desc
    #end




#end












-- #sql("deviceState.findPageAllByResample")
--     SELECT
--         FROM_UNIXTIME(FLOOR(UNIX_TIMESTAMP(create_time) / #para(resample)) * #para(resample)) AS interval_start,
--         ROUND(MAX(power), 2) AS max_power,
--         ROUND(AVG(current), 2) AS avg_current,
--         ROUND(AVG(voltage), 2) AS avg_voltage,
--         ROUND(AVG(active_power), 2) AS avg_active_power,
--         ROUND(AVG(hz), 2) AS avg_hz
--     FROM
--         device_state
--     WHERE
--         deleted = 0
--         AND model IS NOT NULL
--         AND type IS NOT NULL
--         AND voltage > 0
--         #if(model != null)
--             and model = #para(model)
--         #end
--         #if(beginTime != null && endTime != null)
--             and create_time between #para(beginTime) and #para(endTime)
--         #end
--         #if(beginTime != null && endTime == null)
--             and create_time >= #para(beginTime)
--         #end
--         #if(beginTime == null && endTime != null)
--             and create_time <= #para(endTime)
--         #end
--     GROUP BY
--         FROM_UNIXTIME(FLOOR(UNIX_TIMESTAMP(create_time) / #para(resample)) * #para(resample))
-- #end






























