fubin 2 лет назад
Родитель
Сommit
d5cdc71fb7

+ 7 - 0
src/main/java/com/dk/mdm/mapper/mac/AccountItemMapper.java

@@ -35,6 +35,13 @@ public interface AccountItemMapper extends BaseMapper<AccountItem>{
     List<AccountItemResponse> getPayableAccountItem(AccountItemQuery accountItemQuery);
 
     /**
+     * @desc : 查询应收账款明细(锁表)
+     * @author : 付斌
+     * @date : 2024-02-28 10:18
+     */
+    List<AccountItemResponse> getReceivableAccountItemForUpdate(AccountItemQuery accountItemQuery);
+
+    /**
      * @desc   : 查询收款总额
      * @author : 付斌
      * @date   : 2024-03-09 11:36

+ 116 - 79
src/main/java/com/dk/mdm/mapper/mac/AccountItemMapper.xml

@@ -194,48 +194,48 @@
     <!-- 查询应收账款明细 -->
     <select id="getReceivableAccountItem" resultMap="BaseResultMapResponse">
         SELECT t.item_id,
-        t.item_id as "accItemId",
-        t.acc_item_type,
-        t.object_id,
-        tmc.cus_code as "cusCode",
-        tmc.cus_name as "cusName",
-        tmc.cus_phone as "cusPhone",
-        tmc.address_full as "addressFull",
-        t.org_id,
-        tmo.org_name as "orgName",
-        t.staff_id,
-        tms.staff_name as "staffName",
-        t.biznis_type,
-        t.biznis_id,
-        t.biznis_no,
-        t.acc_date,
-        t.rec_status,
-        sys.f_get_name_i18n(tdk1.kind_name_i18n, #{i18n}) as "recStatusName",
-        coalesce(sys.f_get_name_i18n(tdk2.kind_name_i18n, #{i18n}), '其他收入') as "biznisTypeName",
-        t.amt_rec,
-        t.amt_pay,
-        t.amt_should,
-        t.amt_handle,
-        t.amt_waive,
-        t.amt_residue as "amtNotHandle",
-        t.amt_residue as "amtReceivableHandle",
-        t.amt_residue,
-        t.remarks,
-        t.make_staff,
-        t.make_time,
-        t.flg_valid,
-        t.cp_id
+               t.item_id        as "accItemId",
+               t.acc_item_type,
+               t.object_id,
+               tmc.cus_code     as "cusCode",
+               tmc.cus_name     as "cusName",
+               tmc.cus_phone    as "cusPhone",
+               tmc.address_full as "addressFull",
+               t.org_id,
+               tmo.org_name     as "orgName",
+               t.staff_id,
+               tms.staff_name   as "staffName",
+               t.biznis_type,
+               t.biznis_id,
+               t.biznis_no,
+               t.acc_date,
+               t.rec_status,
+               sys.f_get_name_i18n(tdk1.kind_name_i18n, #{i18n}) as "recStatusName",
+               coalesce(sys.f_get_name_i18n(tdk2.kind_name_i18n, #{i18n}), '其他收入') as "biznisTypeName",
+               t.amt_rec,
+               t.amt_pay,
+               t.amt_should,
+               t.amt_handle,
+               t.amt_waive,
+               t.amt_residue    as "amtNotHandle",
+               t.amt_residue    as "amtReceivableHandle",
+               t.amt_residue,
+               t.remarks,
+               t.make_staff,
+               t.make_time,
+               t.flg_valid,
+               t.cp_id
         FROM dkic_b.t_mac_account_item as t
-        left join dkic_b.t_psi_outbound as tpo on tpo.out_id = t.biznis_id and t.biznis_type = 't_psi_outbound'
-        LEFT JOIN sys.t_data_kind tdk2 ON tdk2.kind_code = tpo.out_type
-        left join dkic_b.t_mac_other_receivable as tmor
-        on tmor.receivable_id = t.biznis_id and t.biznis_type = 't_mac_other_receivable'
-        left join dkic_b.t_mst_customer tmc on tmc.cus_id = t.object_id
-        left join dkic_b.t_mst_org tmo on tmo.org_id = t.org_id
-        left join dkic_b.t_mst_staff tms on tms.staff_id = t.staff_id
-        left join sys.t_data_kind as tdk1 on t.rec_status = tdk1.kind_code
+                 left join dkic_b.t_psi_outbound as tpo on tpo.out_id = t.biznis_id and t.biznis_type = 't_psi_outbound'
+                 LEFT JOIN sys.t_data_kind tdk2 ON tdk2.kind_code = tpo.out_type
+                 left join dkic_b.t_mac_other_receivable as tmor
+                           on tmor.receivable_id = t.biznis_id and t.biznis_type = 't_mac_other_receivable'
+                 left join dkic_b.t_mst_customer tmc on tmc.cus_id = t.object_id
+                 left join dkic_b.t_mst_org tmo on tmo.org_id = t.org_id
+                 left join dkic_b.t_mst_staff tms on tms.staff_id = t.staff_id
+                 left join sys.t_data_kind as tdk1 on t.rec_status = tdk1.kind_code
         where t.flg_valid
-        and t.acc_item_type = '账款类型-应收'
+          and t.acc_item_type = '账款类型-应收'
         and t.amt_residue <![CDATA[ <> ]]> 0
         <include refid="Condition"/>
         order by t.acc_date
@@ -247,45 +247,45 @@
     <!-- 查询应付账款明细 -->
     <select id="getPayableAccountItem" resultMap="BaseResultMapResponse">
         SELECT t.item_id,
-        t.item_id as "accItemId",
-        t.acc_item_type,
-        t.object_id,
-        supplier.sup_code as "supCode",
-        supplier.sup_name as "supName",
-        t.org_id,
-        tmo.org_name as "orgName",
-        t.staff_id,
-        tms.staff_name as "staffName",
-        t.biznis_type,
-        t.biznis_id,
-        t.biznis_no,
-        t.acc_date,
-        t.rec_status,
-        sys.f_get_name_i18n(tdk1.kind_name_i18n, #{i18n}) as "recStatusName",
-        sys.f_get_name_i18n(tdk2.kind_name_i18n, #{i18n}) as "biznisTypeName",
-        t.amt_rec,
-        t.amt_pay,
-        t.amt_should,
-        t.amt_handle,
-        t.amt_waive,
-        t.amt_residue,
-        t.amt_residue as "amtNotHandle",
-        t.amt_residue as "amtPayableHandle",
-        t.remarks,
-        t.make_staff,
-        t.make_time,
-        t.flg_valid,
-        t.cp_id
+               t.item_id         as "accItemId",
+               t.acc_item_type,
+               t.object_id,
+               supplier.sup_code as "supCode",
+               supplier.sup_name as "supName",
+               t.org_id,
+               tmo.org_name      as "orgName",
+               t.staff_id,
+               tms.staff_name    as "staffName",
+               t.biznis_type,
+               t.biznis_id,
+               t.biznis_no,
+               t.acc_date,
+               t.rec_status,
+               sys.f_get_name_i18n(tdk1.kind_name_i18n, #{i18n}) as "recStatusName",
+               sys.f_get_name_i18n(tdk2.kind_name_i18n, #{i18n}) as "biznisTypeName",
+               t.amt_rec,
+               t.amt_pay,
+               t.amt_should,
+               t.amt_handle,
+               t.amt_waive,
+               t.amt_residue,
+               t.amt_residue     as "amtNotHandle",
+               t.amt_residue     as "amtPayableHandle",
+               t.remarks,
+               t.make_staff,
+               t.make_time,
+               t.flg_valid,
+               t.cp_id
         FROM dkic_b.t_mac_account_item as t
-        left join dkic_b.t_psi_inbound as tpi on tpi.into_id = t.biznis_id and t.biznis_type = 't_psi_inbound'
-        LEFT JOIN sys.t_data_kind tdk2 ON tdk2.kind_code = tpi.into_type
-        left join dkic_b.t_mst_supplier supplier on supplier.sup_id = t.object_id
-        left join dkic_b.t_mst_org tmo on tmo.org_id = t.org_id
-        left join dkic_b.t_mst_staff tms on tms.staff_id = t.staff_id
-        left join sys.t_data_kind as tdk1 on t.rec_status = tdk1.kind_code
+                 left join dkic_b.t_psi_inbound as tpi on tpi.into_id = t.biznis_id and t.biznis_type = 't_psi_inbound'
+                 LEFT JOIN sys.t_data_kind tdk2 ON tdk2.kind_code = tpi.into_type
+                 left join dkic_b.t_mst_supplier supplier on supplier.sup_id = t.object_id
+                 left join dkic_b.t_mst_org tmo on tmo.org_id = t.org_id
+                 left join dkic_b.t_mst_staff tms on tms.staff_id = t.staff_id
+                 left join sys.t_data_kind as tdk1 on t.rec_status = tdk1.kind_code
         where t.flg_valid
-        and t.acc_item_type = '账款类型-应付'
-        and t.amt_residue <![CDATA[ <> ]]> 0
+          and t.acc_item_type = '账款类型-应付'
+          and t.amt_residue <![CDATA[<>]]> 0
         <include refid="Condition"/>
         order by t.acc_date
         <if test="pageSize != null and currentPage != null and pageSize != 0 and currentPage != 0">
@@ -293,6 +293,43 @@
         </if>
     </select>
 
+    <!-- 查询应收账款明细 -->
+    <select id="getReceivableAccountItemForUpdate" resultMap="BaseResultMapResponse">
+        SELECT t.item_id,
+               t.item_id     as "accItemId",
+               t.acc_item_type,
+               t.object_id,
+               t.org_id,
+               t.staff_id,
+               t.biznis_type,
+               t.biznis_id,
+               t.biznis_no,
+               t.acc_date,
+               t.rec_status,
+               t.amt_rec,
+               t.amt_pay,
+               t.amt_should,
+               t.amt_handle,
+               t.amt_waive,
+               t.amt_residue as "amtNotHandle",
+               t.amt_residue as "amtReceivableHandle",
+               t.amt_residue,
+               t.remarks,
+               t.make_staff,
+               t.make_time,
+               t.flg_valid,
+               t.cp_id
+        FROM dkic_b.t_mac_account_item as t
+                 inner join dkic_b.t_psi_outbound as tpo on tpo.out_id = t.biznis_id and t.biznis_type = 't_psi_outbound'
+        where t.flg_valid
+          and t.acc_item_type = '账款类型-应收'
+          and t.amt_residue <![CDATA[<>]]> 0
+          AND t.object_id = #{objectId}::uuid
+          AND tpo.from_id = #{orderId}::uuid
+        order by t.acc_date
+            for update
+    </select>
+
     <!-- 根据主键查询表t_mac_account_item的一行数据 -->
     <select id="selectById" resultMap="BaseResultMap">
         SELECT
@@ -408,7 +445,7 @@
         SELECT COALESCE(sum(tpo.sum_amount - tpo.out_amt), 0) as "sumAmtOrder"
         FROM dkic_b.t_psi_order as tpo
         where tpo.flg_valid
-          and tpo.out_status in('出库状态-出库中','出库状态-待出库')
+          and tpo.out_status in ('出库状态-出库中', '出库状态-待出库')
           and tpo.cus_id = #{id}::uuid
     </select>
 
@@ -417,7 +454,7 @@
         SELECT COALESCE(sum(tpp.sum_amount - tpp.into_amt), 0) as "sumAmtPur"
         FROM dkic_b.t_psi_purchase as tpp
         where tpp.flg_valid
-          and tpp.into_status in('入库状态-待入库','入库状态-入库中')
+          and tpp.into_status in ('入库状态-待入库', '入库状态-入库中')
           and tpp.sup_id = #{id}::uuid
     </select>
 

+ 1 - 1
src/main/java/com/dk/mdm/mapper/sale/OrderMapper.xml

@@ -679,7 +679,7 @@
                  left join sys.t_data_kind as tdk1 on t.order_type = tdk1.kind_code
                  left join sys.t_data_kind as tdk2 on t.order_status = tdk2.kind_code
                  left join sys.t_data_kind as tdk3 on t.out_status = tdk3.kind_code
-        WHERE t.order_id = #{orderId}::uuid
+        WHERE t.order_id = #{id}::uuid
     </select>
 
     <!-- 根据主键锁定表t_psi_order的一行数据 -->

+ 68 - 64
src/main/java/com/dk/mdm/service/mac/AccountService.java

@@ -380,76 +380,80 @@ public class AccountService extends BaseService<Account> {
                                 .setObjectId(objectId);
                         super.updateByUuid(accountUpdate);
 
-                        List<AccountItemResponse> accountItemResponseList = accountItemMapper.getReceivableAccountItem(
+                        List<AccountItemResponse> accountItemResponseList = accountItemMapper.getReceivableAccountItemForUpdate(
                                 new AccountItemQuery().setObjectId(objectId).setOrderId(orderForUpdate.getOrderId()));
 
-                        // 锁定金额
-                        BigDecimal lockAmt = recPayForUpdate.getSumAmtRec();
-                        // 记录核销了哪些应收账
-                        List<RecPayHandleItem> receivableList = new ArrayList<>();
-                        for (AccountItemResponse accountItemResponse : accountItemResponseList) {
-                            RecPayHandleItem recPayHandleItem = new RecPayHandleItem();
-                            recPayHandleItem.setRpId(recPayForUpdate.getRpId()).setCpId(recPayForUpdate.getCpId()).setAccDate(recPayForUpdate.getAccDate())
-                                .setAccItemId(accountItemResponse.getAccItemId());
-                            // 如果定金比应收金额大
-                            if(lockAmt.compareTo(accountItemResponse.getAmtResidue()) == 1){
-                                recPayHandleItem.setAmtReceivableHandle(accountItemResponse.getAmtResidue());
-                                lockAmt = lockAmt.subtract(accountItemResponse.getAmtResidue());
-                            }else{
-                                recPayHandleItem.setAmtReceivableHandle(lockAmt);
-                                lockAmt = BigDecimal.ZERO;
+                        if(accountItemResponseList.size() > 0){
+                            // 记录核销了哪些应收账
+                            List<RecPayHandleItem> receivableList = new ArrayList<>();
+
+                            // 锁定金额
+                            BigDecimal lockAmt = recPayForUpdate.getSumAmtRec();
+
+                            for (AccountItemResponse accountItemResponse : accountItemResponseList) {
+                                RecPayHandleItem recPayHandleItem = new RecPayHandleItem();
+                                recPayHandleItem.setRpId(recPayForUpdate.getRpId()).setCpId(recPayForUpdate.getCpId()).setAccDate(recPayForUpdate.getAccDate())
+                                        .setAccItemId(accountItemResponse.getAccItemId());
+                                // 如果定金比应收金额大
+                                if(lockAmt.compareTo(accountItemResponse.getAmtResidue()) == 1){
+                                    recPayHandleItem.setAmtReceivableHandle(accountItemResponse.getAmtResidue());
+                                    lockAmt = lockAmt.subtract(accountItemResponse.getAmtResidue());
+                                }else{
+                                    recPayHandleItem.setAmtReceivableHandle(lockAmt);
+                                    lockAmt = BigDecimal.ZERO;
+                                }
+                                recPayHandleItemMapper.insert(recPayHandleItem);
+                                receivableList.add(recPayHandleItem);
+
+                                // 账款明细的核销金额和优惠金额
+                                AccountItem accountItemForUpdate = accountItemMapper.selectByIdForUpdate(recPayHandleItem.getAccItemId());
+                                AccountItem accountItemUpdate = new AccountItem();
+                                // 核销金额,超出剩余应收金额
+                                if (accountItemForUpdate.getAmtResidue().compareTo(recPayHandleItem.getAmtReceivableHandle()) == -1) {
+                                    throw new BaseBusinessException(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.AMT_HANDLE_NO_LESS_AMT_SHOULD.getMessage());
+                                }
+                                accountItemUpdate.setAmtHandle(accountItemForUpdate.getAmtHandle().add(recPayHandleItem.getAmtReceivableHandle()))
+                                        .setItemId(recPayHandleItem.getAccItemId());
+                                // 剩余金额 = 应收金额-应收收款金额
+                                accountItemUpdate.setAmtResidue(accountItemForUpdate.getAmtShould().subtract(accountItemUpdate.getAmtHandle()));
+                                accountItemService.updateByUuid(accountItemUpdate);
+
+                                // 如果定金都用完了,跳出循环
+                                if(lockAmt.compareTo(BigDecimal.ZERO)==0){
+                                    break;
+                                }
                             }
-                            recPayHandleItemMapper.insert(recPayHandleItem);
-                            receivableList.add(recPayHandleItem);
 
-                            // 账款明细的核销金额和优惠金额
-                            AccountItem accountItemForUpdate = accountItemMapper.selectByIdForUpdate(recPayHandleItem.getAccItemId());
-                            AccountItem accountItemUpdate = new AccountItem();
-                            // 核销金额,超出剩余应收金额
-                            if (accountItemForUpdate.getAmtResidue().compareTo(recPayHandleItem.getAmtReceivableHandle()) == -1) {
-                                throw new BaseBusinessException(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.AMT_HANDLE_NO_LESS_AMT_SHOULD.getMessage());
-                            }
-                            accountItemUpdate.setAmtHandle(accountItemForUpdate.getAmtHandle().add(recPayHandleItem.getAmtReceivableHandle()))
-                                    .setItemId(recPayHandleItem.getAccItemId());
-                            // 剩余金额 = 应收金额-应收收款金额
-                            accountItemUpdate.setAmtResidue(accountItemForUpdate.getAmtShould().subtract(accountItemUpdate.getAmtHandle()));
-                            accountItemService.updateByUuid(accountItemUpdate);
-                            
-                            // 如果定金都用完了,跳出循环
-                            if(lockAmt.compareTo(BigDecimal.ZERO)==0){
-                                break;
+                            // 计算明细的核销金额,优惠金额合计
+                            RecPayHandleItem recPayHandleItem = receivableList.stream().reduce((x, y) -> {
+                                RecPayHandleItem item = new RecPayHandleItem();
+                                item.setAmtReceivableHandle(x.getAmtReceivableHandle().add(y.getAmtReceivableHandle()));
+                                return item;
+                            }).get();
+
+                            // 更新收款单
+                            recPayUpdate = new RecPay();
+                            recPayUpdate.setSumAmtReceivableHandle(recPayHandleItem.getAmtReceivableHandle())
+                                    .setSumShouldHandle(recPayHandleItem.getAmtReceivableHandle())
+                                    .setRpId(recPayForUpdate.getRpId());
+                            receiptService.updateByUuid(recPayUpdate);
+
+                            // 更新总账上
+                            accountForUpdate = accountMapper.selectByIdForUpdate(recPayForUpdate.getObjectId());
+                            accountUpdate = new Account();
+                            accountUpdate.setReceivableHandle(accountForUpdate.getReceivableHandle().add(recPayHandleItem.getAmtReceivableHandle()))// 总应收收款金额
+                                    .setObjectId(accountForUpdate.getObjectId());
+                            // 剩余应收 = 总应收账款-总应收收款金额
+                            accountUpdate.setReceivableResidue(accountForUpdate.getReceivable().subtract(accountUpdate.getReceivableHandle()));
+                            // 可退金额 = 总收款金额-总应收收款金额+总应收优惠金额
+                            accountUpdate.setReceiptResidue(accountForUpdate.getReceipt().subtract(accountUpdate.getReceivableHandle()));
+
+                            // 可用金额为负数,则不能保存
+                            if (accountUpdate.getReceiptResidue().compareTo(BigDecimal.ZERO) == -1) {
+                                throw new BaseBusinessException(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.MAC_BALANCE_NO_LESS.getMessage());
                             }
+                            super.updateByUuid(accountUpdate);
                         }
-
-                        // 计算明细的核销金额,优惠金额合计
-                        RecPayHandleItem recPayHandleItem = receivableList.stream().reduce((x, y) -> {
-                            RecPayHandleItem item = new RecPayHandleItem();
-                            item.setAmtReceivableHandle(x.getAmtReceivableHandle().add(y.getAmtReceivableHandle()));
-                            return item;
-                        }).get();
-
-                        // 更新收款单
-                        recPayUpdate = new RecPay();
-                        recPayUpdate.setSumAmtReceivableHandle(recPayHandleItem.getAmtReceivableHandle())
-                                .setSumShouldHandle(recPayHandleItem.getAmtReceivableHandle())
-                                .setRpId(recPayForUpdate.getRpId());
-                        receiptService.updateByUuid(recPayUpdate);
-
-                        // 更新总账上
-                        accountForUpdate = accountMapper.selectByIdForUpdate(recPayForUpdate.getObjectId());
-                        accountUpdate = new Account();
-                        accountUpdate.setReceivableHandle(accountForUpdate.getReceivableHandle().add(recPayHandleItem.getAmtReceivableHandle()))// 总应收收款金额
-                                .setObjectId(accountForUpdate.getObjectId());
-                        // 剩余应收 = 总应收账款-总应收收款金额
-                        accountUpdate.setReceivableResidue(accountForUpdate.getReceivable().subtract(accountUpdate.getReceivableHandle()));
-                        // 可退金额 = 总收款金额-总应收收款金额+总应收优惠金额
-                        accountUpdate.setReceiptResidue(accountForUpdate.getReceipt().subtract(accountUpdate.getReceivableHandle()));
-
-                        // 可用金额为负数,则不能保存
-                        if (accountUpdate.getReceiptResidue().compareTo(BigDecimal.ZERO) == -1) {
-                            throw new BaseBusinessException(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.MAC_BALANCE_NO_LESS.getMessage());
-                        }
-                        super.updateByUuid(accountUpdate);
                     }
                 }
             }