fubin hace 2 años
padre
commit
fe5552432e

+ 82 - 0
src/main/java/com/dk/mdm/controller/mac/PaymentController.java

@@ -0,0 +1,82 @@
+package com.dk.mdm.controller.mac;
+
+import com.dk.common.model.pojo.PageList;
+import com.dk.common.response.ResponseResultVO;
+import com.dk.common.service.BaseService;
+import com.dk.mdm.model.pojo.mac.RecPay;
+import com.dk.mdm.model.query.mac.RecPayQuery;
+import com.dk.mdm.model.response.mac.RecPayResponse;
+import com.dk.mdm.model.vo.mac.RecPayVO;
+import com.dk.mdm.service.mac.PaymentService;
+import io.swagger.annotations.Api;
+import io.swagger.annotations.ApiOperation;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.web.bind.annotation.*;
+
+import java.util.Map;
+
+@Api(tags = "收付款单API接口")
+@RestController
+@RequestMapping("/mac/payment")
+public class PaymentController {
+
+    public BaseService<RecPay> getService() {
+        return paymentService;
+    }
+
+    @Autowired
+    private PaymentService paymentService;
+    /**
+     * @desc : 条件查询
+     * @author : 付斌
+     * @date : 2023/1/9 10:36
+     */
+    @ApiOperation(value = "分页、关联、条件查询", notes = "分页、关联、条件查询")
+    @PostMapping({"list_by"})
+    public ResponseResultVO<PageList<RecPayResponse>> selectPaymentByCond(@RequestBody RecPayQuery recPayQuery) {
+        return paymentService.selectPaymentByCond(recPayQuery);
+    }
+
+    /**
+     * @desc :查询付款明细(货物、付款、附件)
+     * @author : 付斌
+     * @date : 2024-02-28 13:24
+     */
+    @PostMapping({"select_rp_info_by_id/{id}"})
+    public ResponseResultVO<Map<String, Object>> selectRpInfoById(@PathVariable String id) {
+        return paymentService.selectRpInfoById(id);
+    }
+
+    /**
+     * @desc : 通过ID查询
+     * @author : 付斌
+     * @date : 2023/1/9 10:41
+     */
+    @PostMapping({"/{id}"})
+    public ResponseResultVO selectById(@PathVariable String id) {
+        return paymentService.selectById(id);
+    }
+
+    /**
+     * @desc : 新建应付付款(付款+冲应付)
+     * @author : 付斌
+     * @date : 2023/1/9 10:48
+     */
+    @ApiOperation(value = "新建应付付款", notes = "新建应付付款")
+    @PostMapping({"insert_receivable_payment"})
+    public ResponseResultVO<?> insertPayablePayment(@RequestBody RecPayVO recPayVO) {
+        return paymentService.insertPayablePayment(recPayVO);
+    }
+
+    /**
+     * @desc : 作废
+     * @author : 付斌
+     * @date : 2024-03-08 16:36
+     */
+    @ApiOperation(value = "作废", notes = "作废")
+    @PostMapping({"invalid/{id}"})
+    public ResponseResultVO<?> invalid(@PathVariable String id) {
+        return paymentService.invalid(id);
+    }
+
+}

+ 14 - 0
src/main/java/com/dk/mdm/mapper/mac/RecPayMapper.java

@@ -33,5 +33,19 @@ public interface RecPayMapper extends BaseMapper<RecPay>{
      * @date   : 2024-03-03 9:25
      */
     RecPayResponse selectById(String id);
+
+    /**
+     * @desc   : 根据条件进行查询
+     * @author : 付斌
+     * @date   : 2024-02-28 10:18
+     */
+    List<RecPayResponse> selectPaymentByCond(RecPayQuery recPayQuery);
+
+    /**
+     * @desc   : 根据条件进行查询(数量)
+     * @author : 付斌
+     * @date   : 2024-02-28 10:19
+     */
+    Long countPaymentByCond(RecPayQuery recPayQuery);
 }
 

+ 178 - 134
src/main/java/com/dk/mdm/mapper/mac/RecPayMapper.xml

@@ -62,97 +62,98 @@
 
     <!-- 通用条件列 -->
     <sql id="Condition">
-        <where>
-            <if test="rpNo != null and rpNo != ''">
-                AND t.rp_no LIKE concat('%',my_ex.likequery(#{rpNo}),'%')
-            </if>
-            <if test="searchText !=null and searchText != ''">
-                AND (
-                t.rp_no   LIKE concat('%',my_ex.likequery(#{searchText}),'%')
-                or tmc.cus_phone LIKE concat('%',my_ex.likequery(#{searchText}),'%')
-                or tmc.cus_name LIKE concat('%',my_ex.likequery(#{searchText}),'%')
-                or tmo.org_name LIKE concat('%',my_ex.likequery(#{searchText}),'%')
-                or tmp.sup_name LIKE concat('%',my_ex.likequery(#{searchText}),'%')
-                or tms.staff_name LIKE concat('%',my_ex.likequery(#{searchText}),'%')
-                )
-            </if>
-            <if test="rpType != null and rpType != ''">
-                AND t.rp_type = #{rpType}
-            </if>
-            <if test="objectId != null and objectId != ''">
-                AND t.object_id = #{objectId}
-            </if>
-            <if test="orgId != null and orgId != ''">
-                AND t.org_id = #{orgId}
-            </if>
-            <if test="staffId != null and staffId != ''">
-                AND t.staff_id = #{staffId}
-            </if>
-            <if test="sumAmtRec != null">
-                AND t.sum_amt_rec = #{sumAmtRec}
-            </if>
-            <if test="sumAmtPay != null">
-                AND t.sum_amt_pay = #{sumAmtPay}
-            </if>
-            <if test="sumAmtReceivableHandle != null">
-                AND t.sum_amt_receivable_handle = #{sumAmtReceivableHandle}
-            </if>
-            <if test="sumAmtPayableHandle != null">
-                AND t.sum_amt_payable_handle = #{sumAmtPayableHandle}
-            </if>
-            <if test="sumWaiveAmt != null">
-                AND t.sum_waive_amt = #{sumWaiveAmt}
-            </if>
-            <if test="accDate != null">
-                AND t.acc_date = #{accDate}
-            </if>
-            <if test="remarks != null and remarks != ''">
-                AND t.remarks = #{remarks}
-            </if>
-            <if test="annexPaths != null and annexPaths != ''">
-                AND t.annex_paths = #{annexPaths}
-            </if>
-            <if test="biznisType != null and biznisType != ''">
-                AND t.biznis_type = #{biznisType}
-            </if>
-            <if test="biznisId != null and biznisId != ''">
-                AND t.biznis_id = #{biznisId}
-            </if>
-            <if test="biznisNo != null and biznisNo != ''">
-                AND t.biznis_no = #{biznisNo}
-            </if>
-            <if test="flgLock != null">
-                AND t.flg_lock = #{flgLock}
-            </if>
-            <if test="makeStaff != null and makeStaff != ''">
-                AND t.make_staff = #{makeStaff}
-            </if>
-            <if test="makeTime != null">
-                AND t.make_time = #{makeTime}
-            </if>
-            <if test="cpId != null">
-                AND t.cp_id = #{cpId}
-            </if>
-            <if test="cusPhone != null and cusPhone != ''">
-                AND tmc.cus_phone LIKE concat('%',my_ex.likequery(#{cusPhone}),'%')
-            </if>
-            <if test="cusName != null and cusName != ''">
-                AND tmc.cus_name LIKE concat('%',my_ex.likequery(#{cusName}),'%')
-            </if>
-            <if test="orgIds != null and orgIds.size() > 0">
-                AND t.org_id  =any(#{orgIds, typeHandler=UuidListTypeHandler})
-            </if>
-            <if test="staffIds != null and staffIds.size() > 0">
-                AND t.staff_id  =any(#{staffIds, typeHandler=UuidListTypeHandler})
-            </if>
-            <if test="makeTimeStart != null and makeTimeEnd != null">
-                AND t.make_time &gt;= #{makeTimeStart}::timestamp with time zone
-                AND t.make_time &lt; #{makeTimeEnd}::timestamp with time zone + interval '1 day'
-            </if>
-            <if test="flgValidList != null and flgValidList.size()>0">
-                AND t.flg_valid =any(#{flgValidList, typeHandler=BooleanListTypeHandler})
-            </if>
-        </where>
+        <if test="rpNo != null and rpNo != ''">
+            AND t.rp_no LIKE concat('%',my_ex.likequery(#{rpNo}),'%')
+        </if>
+        <if test="searchText !=null and searchText != ''">
+            AND (
+            t.rp_no   LIKE concat('%',my_ex.likequery(#{searchText}),'%')
+            or tmc.cus_phone LIKE concat('%',my_ex.likequery(#{searchText}),'%')
+            or tmc.cus_name LIKE concat('%',my_ex.likequery(#{searchText}),'%')
+            or tmo.org_name LIKE concat('%',my_ex.likequery(#{searchText}),'%')
+            or tmp.sup_name LIKE concat('%',my_ex.likequery(#{searchText}),'%')
+            or tms.staff_name LIKE concat('%',my_ex.likequery(#{searchText}),'%')
+            )
+        </if>
+        <if test="rpType != null and rpType != ''">
+            AND t.rp_type = #{rpType}
+        </if>
+        <if test="objectId != null and objectId != ''">
+            AND t.object_id = #{objectId}
+        </if>
+        <if test="orgId != null and orgId != ''">
+            AND t.org_id = #{orgId}
+        </if>
+        <if test="staffId != null and staffId != ''">
+            AND t.staff_id = #{staffId}
+        </if>
+        <if test="sumAmtRec != null">
+            AND t.sum_amt_rec = #{sumAmtRec}
+        </if>
+        <if test="sumAmtPay != null">
+            AND t.sum_amt_pay = #{sumAmtPay}
+        </if>
+        <if test="sumAmtReceivableHandle != null">
+            AND t.sum_amt_receivable_handle = #{sumAmtReceivableHandle}
+        </if>
+        <if test="sumAmtPayableHandle != null">
+            AND t.sum_amt_payable_handle = #{sumAmtPayableHandle}
+        </if>
+        <if test="sumWaiveAmt != null">
+            AND t.sum_waive_amt = #{sumWaiveAmt}
+        </if>
+        <if test="accDate != null">
+            AND t.acc_date = #{accDate}
+        </if>
+        <if test="remarks != null and remarks != ''">
+            AND t.remarks = #{remarks}
+        </if>
+        <if test="annexPaths != null and annexPaths != ''">
+            AND t.annex_paths = #{annexPaths}
+        </if>
+        <if test="biznisType != null and biznisType != ''">
+            AND t.biznis_type = #{biznisType}
+        </if>
+        <if test="biznisId != null and biznisId != ''">
+            AND t.biznis_id = #{biznisId}
+        </if>
+        <if test="biznisNo != null and biznisNo != ''">
+            AND t.biznis_no = #{biznisNo}
+        </if>
+        <if test="flgLock != null">
+            AND t.flg_lock = #{flgLock}
+        </if>
+        <if test="makeStaff != null and makeStaff != ''">
+            AND t.make_staff = #{makeStaff}
+        </if>
+        <if test="makeTime != null">
+            AND t.make_time = #{makeTime}
+        </if>
+        <if test="cpId != null">
+            AND t.cp_id = #{cpId}
+        </if>
+        <if test="cusPhone != null and cusPhone != ''">
+            AND tmc.cus_phone LIKE concat('%',my_ex.likequery(#{cusPhone}),'%')
+        </if>
+        <if test="cusName != null and cusName != ''">
+            AND tmc.cus_name LIKE concat('%',my_ex.likequery(#{cusName}),'%')
+        </if>
+        <if test="orgIdList != null and orgIdList.size() > 0">
+            AND t.org_id  =any(#{orgIdList, typeHandler=UuidListTypeHandler})
+        </if>
+        <if test="staffIdList != null and staffIdList.size() > 0">
+            AND t.staff_id  =any(#{staffIdList, typeHandler=UuidListTypeHandler})
+        </if>
+        <if test="makeTimeStart != null and makeTimeEnd != null">
+            AND t.make_time &gt;= #{makeTimeStart}::timestamp with time zone
+            AND t.make_time &lt; #{makeTimeEnd}::timestamp with time zone + interval '1 day'
+        </if>
+        <if test="flgValidList != null and flgValidList.size()>0">
+            AND t.flg_valid =any(#{flgValidList, typeHandler=BooleanListTypeHandler})
+        </if>
+        <if test="supName != null and supName != ''">
+            AND tmp.sup_name LIKE concat('%',my_ex.likequery(#{supName}),'%')
+        </if>
     </sql>
 
     <sql id="idsForeach">
@@ -170,15 +171,14 @@
                t.rp_type,
                sys.f_get_name_i18n(tdk1.kind_name_i18n, #{i18n}) as "rpTypeName",
                t.object_id,
-                tmp.sup_name                                     AS "supplierName",
-               tmc.cus_code                                      as "cusCode",
-               tmc.cus_name                                      as "cusName",
-               tmc.cus_phone                                     as "cusPhone",
-               tmc.address_full                                  as "addressFull",
+               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",
+               tmo.org_name         as "orgName",
                t.staff_id,
-               tms.staff_name                                    as "staffName",
+               tms.staff_name       as "staffName",
                t.sum_amt_rec,
                t.sum_amt_pay,
                t.sum_amt_receivable_handle,
@@ -192,18 +192,17 @@
                t.biznis_no,
                t.flg_lock,
                t.make_staff,
-               makestaff.staff_name                              as "makeStaffName",
+               makestaff.staff_name as "makeStaffName",
                t.make_time,
                t.flg_valid,
                t.cp_id
         from dkic_b.t_mac_rec_pay as t
-                 left join dkic_b.t_mst_customer tmc
-                           on tmc.cus_id = t.object_id and t.rp_type in ('收付款类型-收款', '收付款类型-退收款')
-                 Left join dkic_b.t_mst_supplier tmp on tmp.sup_id = t.object_id
+                 left join dkic_b.t_mst_customer tmc on tmc.cus_id = t.object_id
                  left join sys.t_data_kind as tdk1 on tdk1.kind_code = t.rp_type
                  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 dkic_b.t_mst_staff as makestaff on makestaff.staff_id = t.make_staff
+        where t.rp_type in ('收付款类型-收款', '收付款类型-退收款')
         <include refid="Condition"/>
         order by t.op_create_time desc
         <if test="pageSize != null and currentPage != null and pageSize != 0 and currentPage != 0">
@@ -213,16 +212,14 @@
 
     <!-- 查询表t_mac_rec_pay,(条件查询)个数 -->
     <select id="countByCond" resultType="Long">
-        SELECT
-        count(1)
-        FROM dkic_b.t_mac_rec_pay as t
-         LEFT JOIN dkic_b.t_mst_customer tmc
-                   ON tmc.cus_id = t.object_id
-        Left join dkic_b.t_mst_supplier tmp on tmp.sup_id = t.object_id
-        left join sys.t_data_kind as tdk1 on tdk1.kind_code = t.rp_type
-        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 dkic_b.t_mst_staff as makestaff on makestaff.staff_id = t.make_staff
+        SELECT count(1)
+        from dkic_b.t_mac_rec_pay as t
+                 left join dkic_b.t_mst_customer tmc on tmc.cus_id = t.object_id
+                 left join sys.t_data_kind as tdk1 on tdk1.kind_code = t.rp_type
+                 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 dkic_b.t_mst_staff as makestaff on makestaff.staff_id = t.make_staff
+        where t.rp_type in ('收付款类型-收款', '收付款类型-退收款')
         <include refid="Condition"/>
     </select>
 
@@ -233,15 +230,14 @@
                t.rp_type,
                sys.f_get_name_i18n(tdk1.kind_name_i18n, #{i18n}) as "rpTypeName",
                t.object_id,
-               tmp.sup_name          AS "supplierName",
-               tmc.cus_code           as "cusCode",
-               tmc.cus_name           as "cusName",
-               tmc.cus_phone          as "cusPhone",
-               tmc.address_full       as "addressFull",
+               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",
+               tmo.org_name         as "orgName",
                t.staff_id,
-               tms.staff_name         as "staffName",
+               tms.staff_name       as "staffName",
                t.sum_amt_rec,
                t.sum_amt_pay,
                t.sum_amt_receivable_handle,
@@ -255,28 +251,18 @@
                t.biznis_no,
                t.flg_lock,
                t.make_staff,
-               makestaff.staff_name   as "makeStaffName",
+               makestaff.staff_name as "makeStaffName",
                t.make_time,
                t.flg_valid,
-               t.cp_id,
-               tma.receipt,
-               tma.receipt_lock       as "receiptLock",
-               tma.contract_assets    as "contractAssets",
-               tma.receivable,
-               tma.receivable_handle  as "receivableHandle",
-               tma.receivable_waive   as "receivableWaive",
-               tma.receivable_residue as "receivableResidue",
-               tma.receipt_residue    as "receiptResidue"
+               t.cp_id
         from dkic_b.t_mac_rec_pay as t
-                 left join dkic_b.t_mst_customer tmc
-                           on tmc.cus_id = t.object_id and t.rp_type in ('收付款类型-收款', '收付款类型-退收款')
-                 Left join dkic_b.t_mst_supplier tmp on tmp.sup_id = t.object_id
+                 left join dkic_b.t_mst_customer tmc on tmc.cus_id = t.object_id
                  left join sys.t_data_kind as tdk1 on tdk1.kind_code = t.rp_type
                  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 dkic_b.t_mst_staff as makestaff on makestaff.staff_id = t.make_staff
-                 left join dkic_b.t_mac_account as tma on tma.object_id = t.object_id and tma.object_type = '对象类型-客户'
-        WHERE t.rp_id = #{id}::uuid
+        where t.rp_type in ('收付款类型-收款', '收付款类型-退收款')
+          and t.rp_id = #{id}::uuid
     </select>
 
     <!-- 根据主键锁定表t_mac_rec_pay的一行数据 -->
@@ -353,4 +339,62 @@
             )
         </foreach>
     </insert>
+
+
+    <!-- 查询表t_mac_rec_pay,(条件查询+分页)列表 -->
+    <select id="selectPaymentByCond" resultMap="BaseResultMapResponse">
+        select t.rp_id,
+               t.rp_no,
+               t.rp_type,
+               sys.f_get_name_i18n(tdk1.kind_name_i18n, #{i18n}) as "rpTypeName",
+               t.object_id,
+               tmp.sup_code         as "supCode",
+               tmp.sup_name         as "supName",
+               t.org_id,
+               tmo.org_name         as "orgName",
+               t.staff_id,
+               tms.staff_name       as "staffName",
+               t.sum_amt_rec,
+               t.sum_amt_pay,
+               t.sum_amt_receivable_handle,
+               t.sum_amt_payable_handle,
+               t.sum_waive_amt,
+               t.acc_date,
+               t.remarks,
+               t.annex_paths,
+               t.biznis_type,
+               t.biznis_id,
+               t.biznis_no,
+               t.flg_lock,
+               t.make_staff,
+               makestaff.staff_name as "makeStaffName",
+               t.make_time,
+               t.flg_valid,
+               t.cp_id
+        from dkic_b.t_mac_rec_pay as t
+                 Left join dkic_b.t_mst_supplier tmp on tmp.sup_id = t.object_id
+                 left join sys.t_data_kind as tdk1 on tdk1.kind_code = t.rp_type
+                 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 dkic_b.t_mst_staff as makestaff on makestaff.staff_id = t.make_staff
+        where t.rp_type in ('收付款类型-付款', '收付款类型-退付款')
+        <include refid="Condition"/>
+        order by t.op_create_time desc
+        <if test="pageSize != null and currentPage != null and pageSize != 0 and currentPage != 0">
+            limit #{end} offset #{start}
+        </if>
+    </select>
+
+    <!-- 查询表t_mac_rec_pay,(条件查询)个数 -->
+    <select id="countPaymentByCond" resultType="Long">
+        SELECT count(1)
+        from dkic_b.t_mac_rec_pay as t
+                 Left join dkic_b.t_mst_supplier tmp on tmp.sup_id = t.object_id
+                 left join sys.t_data_kind as tdk1 on tdk1.kind_code = t.rp_type
+                 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 dkic_b.t_mst_staff as makestaff on makestaff.staff_id = t.make_staff
+        where t.rp_type in ('收付款类型-付款', '收付款类型-退付款')
+        <include refid="Condition"/>
+    </select>
 </mapper>

+ 3 - 15
src/main/java/com/dk/mdm/model/query/mac/RecPayQuery.java

@@ -93,21 +93,6 @@ public class RecPayQuery extends PageInfo<RecPayQuery> implements Serializable {
     private String staffId;
 
     /**
-     * 员工ID
-     */
-    @ApiModelProperty(value = "员工ID")
-    @TableField(typeHandler = UuidListTypeHandler.class)
-    private List<String> staffIds;
-
-    /**
-     * @desc   : 组织部门list
-     */
-    @ApiModelProperty(value = "组织部门list")
-    @TableField(typeHandler = UuidListTypeHandler.class)
-    private List<String> orgIds;
-
-
-    /**
      * 收款金额
      */
     @Excel(name = "收款金额")
@@ -261,6 +246,9 @@ public class RecPayQuery extends PageInfo<RecPayQuery> implements Serializable {
     @ApiModelProperty(value = "有效标识 (1:正常 0:停用)")
     private List<Boolean> flgValidList;
 
+    @ApiModelProperty(value = "供应商名称")
+    private String supName;
+
     private static final long serialVersionUID = 1L;
 
 }

+ 5 - 0
src/main/java/com/dk/mdm/model/response/mac/AccountItemResponse.java

@@ -249,6 +249,11 @@ public class AccountItemResponse extends PageInfo<AccountItemResponse> implement
     @ApiModelProperty(value = "未核销金额")
     private BigDecimal amtNotHandle;
 
+    @ApiModelProperty(value = "供应商编码")
+    private String supCode;
+
+    @ApiModelProperty(value = "供应商名称")
+    private String supName;
 
     private static final long serialVersionUID = 1L;
 

+ 6 - 3
src/main/java/com/dk/mdm/model/response/mac/RecPayResponse.java

@@ -227,9 +227,6 @@ public class RecPayResponse extends PageInfo<RecPayResponse> implements Serializ
     @ApiModelProperty(value = "组织名称")
     private String orgName;
 
-    @ApiModelProperty(value = "供应商")
-    private String supplierName;
-
     @ApiModelProperty(value = "员工姓名")
     private String staffName;
 
@@ -272,6 +269,12 @@ public class RecPayResponse extends PageInfo<RecPayResponse> implements Serializ
     @ApiModelProperty(value = "总收款可退金额 (总收款金额-总应收收款金额+总应收优惠金额(现金池))")
     private BigDecimal receiptResidue;
 
+    @ApiModelProperty(value = "供应商编码")
+    private String supCode;
+
+    @ApiModelProperty(value = "供应商名称")
+    private String supName;
+
     private static final long serialVersionUID = 1L;
 
 }

+ 342 - 0
src/main/java/com/dk/mdm/service/mac/PaymentService.java

@@ -0,0 +1,342 @@
+package com.dk.mdm.service.mac;
+
+import com.dk.common.exception.BaseBusinessException;
+import com.dk.common.infrastructure.annotaiton.Pagination;
+import com.dk.common.infrastructure.constant.Constant;
+import com.dk.common.infrastructure.enums.ErrorCodeEnum;
+import com.dk.common.mapper.BaseMapper;
+import com.dk.common.model.pojo.PageList;
+import com.dk.common.response.ResponseCodeEnum;
+import com.dk.common.response.ResponseResultUtil;
+import com.dk.common.response.ResponseResultVO;
+import com.dk.common.service.BaseService;
+import com.dk.mdm.infrastructure.convert.mac.RecPayConvert;
+import com.dk.mdm.infrastructure.convert.mac.RecPayHandleItemConvert;
+import com.dk.mdm.infrastructure.convert.mac.RecPayItemConvert;
+import com.dk.mdm.mapper.mac.*;
+import com.dk.mdm.mapper.mst.MoneyAccountItemMapper;
+import com.dk.mdm.mapper.mst.MoneyAccountMapper;
+import com.dk.mdm.model.pojo.mac.*;
+import com.dk.mdm.model.pojo.mst.MoneyAccount;
+import com.dk.mdm.model.pojo.mst.MoneyAccountItem;
+import com.dk.mdm.model.query.mac.RecPayHandleItemQuery;
+import com.dk.mdm.model.query.mac.RecPayItemQuery;
+import com.dk.mdm.model.query.mac.RecPayQuery;
+import com.dk.mdm.model.response.mac.RecPayHandleItemResponse;
+import com.dk.mdm.model.response.mac.RecPayItemResponse;
+import com.dk.mdm.model.response.mac.RecPayResponse;
+import com.dk.mdm.model.vo.mac.RecPayHandleItemVO;
+import com.dk.mdm.model.vo.mac.RecPayItemVO;
+import com.dk.mdm.model.vo.mac.RecPayVO;
+import com.dk.mdm.service.common.CommonService;
+import com.dk.mdm.service.mst.MoneyAccountService;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Service;
+import org.springframework.transaction.annotation.Transactional;
+
+import java.math.BigDecimal;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+@Service
+@Transactional
+public class PaymentService extends BaseService<RecPay> {
+
+    @Override
+    public String getPrimaryKey() {
+        return "rp_id";
+    }
+
+    @Override
+    public BaseMapper<RecPay> getRepository() {
+        return recPayMapper;
+    }
+
+    @Autowired
+    private RecPayMapper recPayMapper;
+
+    @Autowired
+    private RecPayItemService recPayItemService;
+
+    @Autowired
+    private RecPayItemMapper recPayItemMapper;
+
+    @Autowired
+    private AccountService accountService;
+
+    @Autowired
+    private AccountMapper accountMapper;
+
+    @Autowired
+    private AccountItemService accountItemService;
+
+    @Autowired
+    private MoneyAccountMapper moneyAccountMapper;
+
+    @Autowired
+    private AccountItemMapper accountItemMapper;
+
+    @Autowired
+    private MoneyAccountService moneyAccountService;
+
+    @Autowired
+    private MoneyAccountItemMapper moneyAccountItemMapper;
+
+    @Autowired
+    private RecPayHandleItemService recPayHandleItemService;
+
+    @Autowired
+    private RecPayHandleItemMapper recPayHandleItemMapper;
+
+    @Autowired
+    private CommonService commonService;
+
+    @Autowired
+    private RecPayConvert recPayConvert;
+
+    @Autowired
+    private RecPayItemConvert recPayItemConvert;
+
+    @Autowired
+    private RecPayHandleItemConvert recPayHandleItemConvert;
+
+    /**
+     * @desc : 条件查询
+     * @author : 付斌
+     * @date : 2023/1/9 10:40
+     */
+    @Pagination
+    public ResponseResultVO<PageList<RecPayResponse>> selectPaymentByCond(RecPayQuery recPayQuery) {
+        return super.mergeListWithCount(recPayQuery, recPayMapper.selectPaymentByCond(recPayQuery),
+                recPayMapper.countPaymentByCond(recPayQuery));
+    }
+
+    /**
+     * @desc : 查询付款明细
+     * @author : 付斌
+     * @date : 2024-02-28 13:25
+     */
+    @Pagination
+    public ResponseResultVO<Map<String, Object>> selectRpInfoById(String id) {
+        Map<String, Object> result = new HashMap<>();
+        // 付款明细
+        List<RecPayItemResponse> recPayItem = recPayItemMapper.selectByCond(new RecPayItemQuery().setRpId(id));
+        result.put("recPayItem", recPayItem);
+
+        // 应付核销明细
+        List<RecPayHandleItemResponse> recPayHandleItem = recPayHandleItemMapper.selectByCond(new RecPayHandleItemQuery().setRpId(id));
+        result.put("recPayHandleItem", recPayHandleItem);
+
+        // 附件
+        return ResponseResultUtil.success(result);
+    }
+    
+    /**
+     * @desc : 新建应付核销(付款+核销应付)
+     * @author : 付斌
+     * @date : 2023/1/9 10:49
+     */
+    @Transactional(
+            rollbackFor = {Exception.class}
+    )
+    public ResponseResultVO<?> insertPayablePayment(RecPayVO recPayVO) {
+
+        /*********************  付款的处理 begin **********************/
+        // 获取单号
+        Map<String, Object> codeMap = commonService.getUniqueNoteCode(Constant.docNameConstant.RECPAY.getName(), false);
+        recPayVO.setRpId(codeMap.get("outId").toString()).setRpNo(codeMap.get("outNote").toString());
+        // 转化实体
+        RecPay recPay = recPayConvert.convertToPo(recPayVO);
+        // 总单保存
+        super.insert(recPay);
+
+        // 明细保存
+        if (recPayVO.getItemList() != null && recPayVO.getItemList().size() > 0) {
+            for (RecPayItemVO recPayItemVO : recPayVO.getItemList()) {
+                RecPayItem recPayItem = recPayItemConvert.convertToPo(recPayItemVO);
+                recPayItem.setRpId(recPay.getRpId()).setCpId(recPay.getCpId()).setMakeStaff(recPay.getMakeStaff()).setAccDate(recPay.getAccDate());
+                recPayItemMapper.insert(recPayItem);
+
+                // 插入账款明细
+                AccountItem accountItem = new AccountItem();
+                accountItem.setAccItemType(Constant.accItemType.SHOU_KUAN.getName())
+                        .setObjectId(recPayVO.getObjectId()).setOrgId(recPayVO.getOrgId()).setStaffId(recPayVO.getStaffId())
+                        .setAccDate(recPayVO.getAccDate()).setRecStatus(Constant.recStatuse.QUE_DING.getName()).setAmtRec(recPayItem.getAmtRec())
+                        .setBiznisType("t_mac_rec_pay_item").setBiznisId(recPayItem.getItemId()).setBiznisNo(recPayVO.getRpNo())
+                        .setMakeStaff(recPayVO.getMakeStaff()).setCpId(recPayVO.getCpId());
+                accountItemMapper.insert(accountItem);
+
+                // 更新付款单上的账款明细Id
+                RecPayItem recPayItemUpdate = new RecPayItem();
+                recPayItemUpdate.setAccItemId(accountItem.getItemId()).setItemId(recPayItem.getItemId());
+                recPayItemService.updateByUuid(recPayItemUpdate);
+
+                // 插入资金流水
+                MoneyAccountItem moneyAccountItem = new MoneyAccountItem();
+                moneyAccountItem.setMacId(recPayItem.getMacId()).setFlowType(Constant.FlowType.SHOU_FU_KUAN.getName())
+                        .setInvoiceId(recPayItem.getItemId()).setAmtInflow(recPayItem.getAmtRec()).setAccDate(recPayVO.getAccDate())
+                        .setMakeStaff(recPayItem.getMakeStaff()).setCpId(recPayItem.getCpId());
+                moneyAccountItemMapper.insert(moneyAccountItem);
+
+                // 更新资金账户
+                MoneyAccount moneyAccountForUpdate = moneyAccountMapper.selectByIdForUpdate(recPayItem.getMacId());
+                MoneyAccount moneyAccountUpdate = new MoneyAccount();
+                moneyAccountUpdate.setBalance(moneyAccountForUpdate.getBalance().add(recPayItem.getAmtRec()))
+                        .setMacId(moneyAccountForUpdate.getMacId());
+                moneyAccountService.updateByUuid(moneyAccountUpdate);
+            }
+        }
+
+        // 插入账款总表
+        Account accountForUpdate = accountService.getCusAccountForUpdate(recPayVO.getObjectId());
+        // 更新账款总表上付款的相关字段
+        Account accountUpdate = new Account();
+        accountUpdate.setReceipt(accountForUpdate.getReceipt().add(recPayVO.getSumAmtRec()))// 总付款金额
+                .setReceiptResidue(accountForUpdate.getReceiptResidue().add(recPayVO.getSumAmtRec()))//  可退金额
+                .setObjectId(accountForUpdate.getObjectId());
+        accountService.updateByUuid(accountUpdate);
+        /*********************  付款的处理 end **********************/
+
+
+        /*********************  应付付款的处理 begin **********************/
+        // 应付付款的处理
+        if (recPayVO.getReceivableList() != null && recPayVO.getReceivableList().size() > 0) {
+            for (RecPayHandleItemVO recPayHandleItemVO : recPayVO.getReceivableList()) {
+                RecPayHandleItem recPayHandleItem = recPayHandleItemConvert.convertToPo(recPayHandleItemVO);
+                recPayHandleItem.setItemId(null).setRpId(recPay.getRpId()).setCpId(recPay.getCpId()).setAccDate(recPay.getAccDate());
+                recPayHandleItemMapper.insert(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()))
+                        .setAmtWaive(accountItemForUpdate.getAmtWaive().add(recPayHandleItem.getAmtWaive()))
+                        .setItemId(recPayHandleItem.getAccItemId());
+                // 剩余金额 = 应付金额-应付付款金额
+                accountItemUpdate.setAmtResidue(accountItemForUpdate.getAmtShould().subtract(accountItemUpdate.getAmtHandle()));
+                accountItemService.updateByUuid(accountItemUpdate);
+            }
+
+            // 计算明细的核销金额,优惠金额合计
+            RecPayHandleItemVO recPayHandleItemVO = recPayVO.getReceivableList().stream().reduce((x, y) -> {
+                RecPayHandleItemVO item = new RecPayHandleItemVO();
+                item.setAmtReceivableHandle(x.getAmtReceivableHandle().add(y.getAmtReceivableHandle()));
+                item.setAmtWaive(x.getAmtWaive().add(y.getAmtWaive()));
+                return item;
+            }).get();
+
+            // 更新总账上
+            accountForUpdate = accountMapper.selectByIdForUpdate(recPayVO.getObjectId());
+            accountUpdate = new Account();
+            accountUpdate.setReceivableHandle(accountForUpdate.getReceivableHandle().add(recPayHandleItemVO.getAmtReceivableHandle()))// 总应付付款金额
+                    .setReceivableWaive(accountForUpdate.getReceivableWaive().add(recPayHandleItemVO.getAmtWaive()))//  总应付优惠金额
+                    .setObjectId(accountForUpdate.getObjectId());
+            // 剩余应付 = 总应付账款-总应付付款金额
+            accountUpdate.setReceivableResidue(accountForUpdate.getReceivable().subtract(accountUpdate.getReceivableHandle()));
+            // 可退金额 = 总付款金额-总应付付款金额+总应付优惠金额
+            accountUpdate.setReceiptResidue(accountForUpdate.getReceipt().subtract(accountUpdate.getReceivableHandle()).add(accountUpdate.getReceivableWaive()));
+
+            // 更新前的最后校验
+            // 剩余应付为负数,则不能保存
+            if (accountUpdate.getReceivableResidue().compareTo(BigDecimal.ZERO) == -1) {
+                throw new BaseBusinessException(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.AMT_HANDLE_NO_LESS_AMT_SHOULD.getMessage());
+            }
+            // 可用金额为负数,则不能保存
+            if (accountUpdate.getReceiptResidue().compareTo(BigDecimal.ZERO) == -1) {
+                throw new BaseBusinessException(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.MAC_BALANCE_NO_LESS.getMessage());
+            }
+            accountService.updateByUuid(accountUpdate);
+        }
+        /*********************  应付付款的处理 end **********************/
+        return ResponseResultUtil.success();
+    }
+
+    /**
+     * @desc : 作废
+     * @author : 付斌
+     * @date : 2024-03-08 16:38
+     */
+    public ResponseResultVO<?> invalid(String id) {
+        RecPay recPayForUpdate = recPayMapper.selectByIdForUpdate(id);
+        // 并发校验
+        if (!recPayForUpdate.getFlgValid()) {
+            throw new BaseBusinessException(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.ISFLGVALID_FALSE.getMessage());
+        }
+        // 如果所在月份已结账,则不能作废 todo
+
+        // 查出并锁定所有应付核销明细
+        AccountItem accountItemForUpdate;
+        List<RecPayHandleItem> recPayHandleItemForUpdateList = recPayHandleItemMapper.selectByZIdForUpdate(id);
+        for (RecPayHandleItem recPayHandleItemForUpdate : recPayHandleItemForUpdateList) {
+            // 更新账款明细应付付款
+            accountItemForUpdate = accountItemMapper.selectByIdForUpdate(recPayHandleItemForUpdate.getAccItemId());
+            AccountItem accountItemUpdate = new AccountItem();
+            accountItemUpdate.setAmtHandle(accountItemForUpdate.getAmtHandle().subtract(recPayHandleItemForUpdate.getAmtReceivableHandle()))
+                    .setAmtWaive(accountItemForUpdate.getAmtWaive().subtract(recPayHandleItemForUpdate.getAmtWaive()))
+                    .setItemId(recPayHandleItemForUpdate.getAccItemId());
+            accountItemUpdate.setAmtResidue(accountItemForUpdate.getAmtShould().subtract(accountItemUpdate.getAmtHandle()).subtract(accountItemUpdate.getAmtWaive()));
+            accountItemService.updateByUuid(accountItemUpdate);
+
+            // 将核销明细有效标识置为false
+            RecPayHandleItem recPayHandleItemUpdate = new RecPayHandleItem();
+            recPayHandleItemUpdate.setFlgValid(false).setItemId(recPayHandleItemForUpdate.getItemId());
+            recPayHandleItemService.updateByUuid(recPayHandleItemUpdate);
+        }
+
+        // 把总帐上的钱加回来
+        Account accountForUpdate = accountMapper.selectByIdForUpdate(recPayForUpdate.getObjectId());
+        Account accountUpdate = new Account();
+        accountUpdate.setReceivableHandle(accountForUpdate.getReceivableHandle().subtract(recPayForUpdate.getSumAmtReceivableHandle()))// 总应付付款金额
+                .setReceivableWaive(accountForUpdate.getReceivableWaive().subtract(recPayForUpdate.getSumWaiveAmt()))//  总应付优惠金额
+                .setObjectId(accountForUpdate.getObjectId());
+        // 剩余应付 = 总应付账款-总应付付款金额
+        accountUpdate.setReceivableResidue(accountForUpdate.getReceivable().subtract(accountUpdate.getReceivableHandle()));
+        // 可退金额 = 总付款金额-总应付付款金额+总应付优惠金额
+        accountUpdate.setReceiptResidue(accountForUpdate.getReceipt().subtract(accountUpdate.getReceivableHandle()).add(accountUpdate.getReceivableWaive()));
+
+        // 更新前的最后校验
+        // 剩余应付为负数,则不能保存
+        if (accountUpdate.getReceivableResidue().compareTo(BigDecimal.ZERO) == -1) {
+            throw new BaseBusinessException(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.AMT_HANDLE_NO_LESS_AMT_SHOULD.getMessage());
+        }
+        // 可用金额为负数,则不能保存
+        if (accountUpdate.getReceiptResidue().compareTo(BigDecimal.ZERO) == -1) {
+            throw new BaseBusinessException(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.MAC_BALANCE_NO_LESS.getMessage());
+        }
+        accountService.updateByUuid(accountUpdate);
+
+        // 将之前的明细全部删除
+        List<RecPayItem> recPayItemOriginalList = recPayItemMapper.selectByZIdForUpdate(id);
+        // 需要重新计算的资金账户
+        List<String> macList = new ArrayList<>();
+        for (RecPayItem recPayItem : recPayItemOriginalList) {
+            // 删除收付款明细
+            recPayItemMapper.deleteById(recPayItem.getItemId());
+            // 删除账款明细
+            accountItemMapper.deleteById(recPayItem.getAccItemId());
+            // 删除账户流水
+            moneyAccountItemMapper.deleteByInvoiceId(recPayItem.getItemId());
+
+            if (!macList.contains(recPayItem.getMacId())) {
+                macList.add(recPayItem.getMacId());
+            }
+        }
+        // 更新总账表的总付款额和可用额
+        accountService.updateReceipt(recPayForUpdate.getObjectId());
+        // 更新账户余额
+        for (String macId : macList) {
+            accountService.updateMac(macId);
+        }
+        // 作废
+        RecPay recPayUpdate = new RecPay();
+        recPayUpdate.setFlgValid(false).setRpId(id);
+        super.updateByUuid(recPayUpdate);
+        return ResponseResultUtil.success();
+    }
+}