a.id AS "id",
a.approver_id AS "approverId",
a.approvetime AS "approvetime",
a.customerandmeterrela_id AS "customerandmeterrelaId",
a.office_id AS "officeId",
a.lastreading AS "lastreading",
a.lastrecorddate AS "lastrecorddate",
a.metercode AS "metercode",
a.watermeter_id AS "watermeterId",
a.month AS "month",
a.payamount AS "payamount",
a.reading AS "reading",
a.recorddate AS "recorddate",
a.amount AS "amount",
a.state AS "state",
a.waterproperty_id AS "waterpropertyId",
a.year AS "year",
a.account_id AS "accountId",
a.accountname AS "accountname",
a.accountnumber AS "accountnumber",
a.address AS "address",
a.watertype AS "watertype",
a.isprint AS "isprint",
a.ele_no AS "eleNo",
a.iswxsend AS "iswxsend",
a.sendwxdate AS "sendwxdate",
a.create_by AS "createBy",
a.create_date AS "createDate",
a.update_by AS "updateBy",
a.update_date AS "updateDate",
a.remarks AS "remarks",
a.del_flag AS "delFlag",
a.site_id as "siteId",
a.customer_id as "customerId",
c.label as stateName,
CONCAT(a.`year`,'年',LPAD(a.month, 2, 0),'月') as closingName,
d.water_meter_no as "metereleno"
inner join sc_device d on a.watermeter_id = d.id
left join pay_sys_dict c on c.`value` = a.state and c.type ='水量状态'
AND c.customer_id = #{customerId}
and c.site_id = #{siteId}
UPDATE pay_amount_waterusedamount SET
amount = #{amountWaterUsedAmount.amount},
approver_id = #{amountWaterUsedAmount.approverId},
approvetime = #{amountWaterUsedAmount.approvetime},
customerandmeterrela_id = #{amountWaterUsedAmount.customerandmeterrelaId},
office_id = #{amountWaterUsedAmount.officeId},
lastreading = #{amountWaterUsedAmount.lastreading},
lastrecorddate = #{amountWaterUsedAmount.lastrecorddate},
metercode = #{amountWaterUsedAmount.metercode},
watermeter_id = #{amountWaterUsedAmount.watermeterId},
month = #{amountWaterUsedAmount.month},
payamount = #{amountWaterUsedAmount.payamount},
reading = #{amountWaterUsedAmount.reading},
recorddate = #{amountWaterUsedAmount.recorddate},
state = #{amountWaterUsedAmount.state},
waterproperty_id = #{amountWaterUsedAmount.waterpropertyId},
year = #{amountWaterUsedAmount.year},
account_id = #{amountWaterUsedAmount.accountId},
accountname = #{amountWaterUsedAmount.accountname},
accountnumber = #{amountWaterUsedAmount.accountnumber},
address = #{amountWaterUsedAmount.address},
watertype = #{amountWaterUsedAmount.watertype},
update_by = #{amountWaterUsedAmount.updateBy},
update_date = #{amountWaterUsedAmount.updateDate},
remarks = #{amountWaterUsedAmount.remarks},
ele_no = #{amountWaterUsedAmount.eleNo},
isprint = #{amountWaterUsedAmount.isprint},
iswxsend = #{amountWaterUsedAmount.iswxsend},
sendwxdate =#{amountWaterUsedAmount.sendwxdate}
WHERE id = #{amountWaterUsedAmount.id}
INSERT INTO pay_amount_waterusedamount
(customerandmeterrela_id, office_id, lastreading, lastrecorddate, metercode,
watermeter_id, month, payamount, reading, recorddate, amount, state,
waterproperty_id, year, account_id, accountname, accountnumber, address,
watertype, isprint, ele_no, iswxsend, sendwxdate, create_by, create_date,
update_by, update_date, remarks, del_flag, site_id, customer_id)
SELECT
c.id,c.office_id,
ROUND(ifnull(lastwua.reading, IFNULL(d.new_meter_start,0)),2),
ifnull(lastwua.recorddate,ifnull(d.install_time,d.date_create)),c.metercode,
c.watermeter_id,#{month},null,null,null,NULL,1,
c.waterproperty_id,#{year},c.account_id,c.accountname,c.accountnumber,c.address,
c.watertype,null,d.electronic_no,null,null,1,NOW(),
1,now(),'批量生成抄表计划',0,c.site_id,c.customer_id
FROM
pay_base_customerandmeterrela c
left join sc_install_list d on d.device_id =c.watermeter_id
LEFT JOIN pay_amount_waterusedamount amount ON amount.watermeter_id = c.watermeter_id
AND amount.YEAR = #{year}
AND amount.MONTH = #{month}
LEFT JOIN (
SELECT
oldwua.watermeter_id,
oldwua.reading,
oldwua.recorddate
FROM
(
SELECT
max(
waterusedamount.create_date
) AS create_date,
waterusedamount.watermeter_id
FROM
pay_amount_waterusedamount waterusedamount
WHERE
waterusedamount.state = 2
GROUP BY
waterusedamount.watermeter_id
) lastid
LEFT JOIN pay_amount_waterusedamount oldwua ON lastid.create_date = oldwua.create_date
AND oldwua.watermeter_id = lastid.watermeter_id
) lastwua ON c.watermeter_id = lastwua.watermeter_id
WHERE
c.businessstate = 1
AND amount.id IS NULL
AND c.customer_id = #{customerId}
and c.site_id = #{siteId}
limit 500
UPDATE pay_amount_waterusedamount SET
amount = #{amountWaterUsedAmount.amount},
payamount = #{amountWaterUsedAmount.payamount},
reading = #{amountWaterUsedAmount.reading},
recorddate = #{amountWaterUsedAmount.recorddate},
update_date = #{amountWaterUsedAmount.updateDate}
WHERE id = #{amountWaterUsedAmount.id}
INSERT INTO pay_amount_waterusedamount_day (
customerandmeterrela_id,
office_id,
lastreading,
lastrecorddate,
metercode,
watermeter_id,
payamount,
reading,
recorddate,
amount,
waterproperty_id,
account_id,
create_by,
create_date,
update_by,
update_date,
remarks,
del_flag,
site_id,
customer_id,
read_date,
debt
) SELECT
c.id,
c.office_id,
ROUND(ifnull( lastwua.reading, ifnull(sil.new_meter_start,0) ),2),
ifnull( lastwua.recorddate, c.create_date ),
c.metercode,
c.watermeter_id,
NULL,
NULL,
NULL,
NULL,
c.waterproperty_id,
c.account_id,
1,
NOW(),
1,
now(),
'每天抄表计划',
0,
c.site_id,
c.customer_id,
DATE_FORMAT( DATE_SUB( now(), INTERVAL 1 DAY ), '%Y%m%d' ),
NULL
FROM
pay_base_customerandmeterrela c
LEFT JOIN sc_install_list sil ON sil.device_id = c.watermeter_id
LEFT JOIN pay_amount_waterusedamount_day amount ON amount.watermeter_id = c.watermeter_id
AND amount.read_date = DATE_FORMAT( DATE_SUB( now(), INTERVAL 1 DAY ), '%Y%m%d' )
LEFT JOIN (
SELECT
oldwua.watermeter_id,
oldwua.reading,
oldwua.recorddate
FROM
(
SELECT
max( waterusedamount.create_date ) AS create_date,
waterusedamount.watermeter_id
FROM
pay_amount_waterusedamount waterusedamount
WHERE
waterusedamount.state = 2
GROUP BY
waterusedamount.watermeter_id
) lastid
LEFT JOIN pay_amount_waterusedamount oldwua ON lastid.create_date = oldwua.create_date
AND oldwua.watermeter_id = lastid.watermeter_id
) lastwua ON c.watermeter_id = lastwua.watermeter_id
WHERE
c.businessstate = 1
AND amount.id IS NULL
AND c.customer_id = #{customerId}
and c.site_id = #{siteId}
limit 500
UPDATE pay_amount_waterusedamount_day SET
amount = #{amountWaterUsedAmount.amount},
payamount = #{amountWaterUsedAmount.payamount},
reading = #{amountWaterUsedAmount.reading},
recorddate = #{amountWaterUsedAmount.recorddate},
update_date = #{amountWaterUsedAmount.updateDate}
WHERE id = #{amountWaterUsedAmount.id}