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,
ifnull(lastwua.reading, IFNULL(d.new_meter_start,0)),
ifnull(lastwua.recorddate,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.water_meter_no,null,null,1,NOW(),
1,now(),'批量生成抄表计划',0,c.site_id,c.customer_id
FROM
pay_base_customerandmeterrela c
left join sc_device d on d.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,
ifnull( lastwua.reading, ifnull(device.new_meter_start,0) ),
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_device device ON 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}
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}
update sc_device set
metercode=#{metercode},
water_meter_no = #{eleno},
new_meter_start = #{stratcount},
update_by = #{updateBy},
date_update = #{updateDate},
where id=#{id}
insert into pay_device_replace_record
(
watermeter_id,
metercode,
old_eleno,
new_eleno,
old_lastreading,
old_reading,
old_amount,
new_startcount,
create_date,
remarks,
site_id,
customer_id,
year,
month)
value(
#{watermeterId},
#{metercode},
#{oldEleno},
#{newEleno},
#{oldLastreading},
#{oldReading},
#{oldAmount},
#{new_startcount},
#{createDate},
#{remarks},
#{siteId},
#{customerId},
#{year},
#{month})
insert into pay_amount_waterusedamount (
customerandmeterrela_id
,office_id
,lastreading
,lastrecorddate
,metercode
,watermeter_id
,month
,state
,waterproperty_id
,year
,account_id
,accountname
,accountnumber
,watertype
,isprint
,ele_no
,create_by
,create_date
,update_by
,update_date
,remarks
,del_flag
,site_id
,customer_id
)
select
c.id as customerandmeterrela_id
,c.office_id
,ifnull(ifnull(amount.reading,d.new_meter_start),null) as lastreading
,ifnull(ifnull(amount.recorddate,d.date_create),null) as lastrecorddate
,c.metercode
,c.watermeter_id
,#{month}
,1
,p.id as waterproperty_id
,#{year}
,c.account_id
,c.accountname
,c.accountnumber
,c.watertype
,0
,d.water_meter_no as ele_no
,'1'
,now()
,'1'
,now()
,'抄表计划'
,'0'
,#{siteId}
,#{customerId}
from pay_base_customerandmeterrela c
left join pay_base_waterproperty p on c.waterproperty_id=p.id
LEFT JOIN pay_amount_waterusedamount amounts ON amounts.watermeter_id = c.watermeter_id
AND amounts.YEAR = #{year}
AND amounts.MONTH = #{month}
left join sc_device d on c.watermeter_id=d.id
left join (
select
a.watermeter_id,
a.reading,
a.recorddate
from (
select
ele_no
,max(create_date) create_date
from pay_amount_waterusedamount
where state =2
group by ele_no
) aLast
left join pay_amount_waterusedamount a on a.ele_no=aLast.ele_no and a.create_date=aLast.create_date
) amount on amount.watermeter_id=c.watermeter_id
where c.businessstate= 1 and amounts.id is null and c.site_id=#{siteId} and c.customer_id=#{customerId}