Administrator
2023-12-27 73bab98fa6620a7648927fbab9856dd7cb334726
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
package com.dy.pipIrrSell.activeCard;
 
import com.dy.common.aop.SsoAop;
import com.dy.common.webUtil.BaseResponse;
import com.dy.common.webUtil.BaseResponseUtils;
import com.dy.common.webUtil.QueryResultVo;
import com.dy.common.webUtil.ResultCodeMsg;
import com.dy.pipIrrGlobal.pojoSe.SeActiveCard;
import com.dy.pipIrrGlobal.pojoSe.SeClientCard;
import com.dy.pipIrrGlobal.voSe.VoActiveCard;
import com.dy.pipIrrSell.clientCard.CardStateENUM;
import com.dy.pipIrrSell.clientCard.ClientCardSv;
import com.dy.pipIrrSell.clientCard.LastOperateENUM;
import com.dy.pipIrrSell.recharge.DtoRecharge;
import com.dy.pipIrrSell.recharge.RechargeCtrl;
import com.dy.pipIrrSell.result.SellResultCode;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.MediaType;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
 
import java.util.*;
 
/**
 * @author ZhuBaoMin
 * @date 2023/12/5 20:06
 * @LastEditTime 2023/12/5 20:06
 * @Description
 */
 
@Slf4j
@Tag(name = "开卡管理", description = "开卡操作")
@RestController
@RequestMapping(path="activecard")
@RequiredArgsConstructor
public class ActiveCardCtrl {
    private final ActiveCardSv activeCardSv;
    private final ClientCardSv clientCardSv;
    private final RechargeCtrl rechargeCtrl;
 
    @Value("${projectCode.ym}")
    private Integer projectCode;
 
    /**
     * 获取开卡记录
     * @param vo
     * @return
     */
    @Operation(summary = "获得一页开卡记录", description = "返回一页开卡数据")
    @ApiResponses(value = {
            @ApiResponse(
                    responseCode = ResultCodeMsg.RsCode.SUCCESS_CODE,
                    description = "返回一页开卡数据(BaseResponse.content:QueryResultVo[{}])",
                    content = {@Content(mediaType = MediaType.APPLICATION_JSON_VALUE,
                            schema = @Schema(implementation = VoActiveCard.class))}
            )
    })
    //@GetMapping(path = "/getActiveCards", consumes = MediaType.APPLICATION_JSON_VALUE)
    @GetMapping(path = "/getActiveCards")
    @SsoAop()
    public BaseResponse<QueryResultVo<List<VoActiveCard>>> getActiveCards(QueryVo vo){
        try {
            QueryResultVo<List<VoActiveCard>> res = activeCardSv.getActiveCards(vo);
            return BaseResponseUtils.buildSuccess(res);
        } catch (Exception e) {
            log.error("获取开卡记录异常", e);
            return BaseResponseUtils.buildException(e.getMessage()) ;
        }
    }
 
    /**
     * 开卡操作
     * @param po
     * @param bindingResult
     * @return
     */
    @Operation(summary = "开卡", description = "新开农户卡")
    @ApiResponses(value = {
            @ApiResponse(
                    responseCode = ResultCodeMsg.RsCode.SUCCESS_CODE,
                    description = "操作结果:true:成功,false:失败(BaseResponse.content)",
                    content = {@Content(mediaType = MediaType.APPLICATION_JSON_VALUE,
                            schema = @Schema(implementation = Boolean.class))}
            )
    })
    @PostMapping(path = "add_active", consumes = MediaType.APPLICATION_JSON_VALUE)
    @Transactional(rollbackFor = Exception.class)
    @SsoAop()
    public BaseResponse<Boolean> add_active(@RequestBody @Parameter(description = "form表单json数据", required = true) @Valid DtoActiveCard po, @Parameter(hidden = true) BindingResult bindingResult){
        if(bindingResult != null && bindingResult.hasErrors()){
            return BaseResponseUtils.buildFail(Objects.requireNonNull(bindingResult.getFieldError()).getDefaultMessage());
        }
 
        /**
         * 获取5级行政区划串areaCode
         * provinceId   省级区划代码
         * cityId       市级区划代码
         * countryId    县级区划代码
         * townId       镇级区划代码
         * villageId    村级区划代码
         */
        //VoAreaCode voAreaCode = activeCardSv.getAreaCodeByNum(po.getClientNum());
        //if(voAreaCode == null) {
        //    return BaseResponseUtils.buildFail(SellResultCode.AREA_CODE_MISTAKE.getMessage());
        //}
        //String provinceId = voAreaCode.getProvinceId();
        //String cityId = voAreaCode.getCityId();
        //String countryId = voAreaCode.getCountryId();
        //String townId = voAreaCode.getTownId();
        //String villageId = voAreaCode.getVillageId();
        //String areaCode = provinceId + cityId + countryId + townId + villageId;
 
        String areaCode = String.valueOf(activeCardSv.getAreaCodeByNum(po.getClientNum()));
 
        /**
         * 根据行政区划串(areaCode)在水卡表中针对水卡编号(cardNum)进行模糊查询
         * 如果4位顺序号已经达到最大值,提示用户联系系统管理员
         * 如果4位顺序号未达到最大值,则加1
         * cardNum为新的卡号
         */
        String cardNum = Optional.ofNullable(activeCardSv.getCardNumOfMax(areaCode)).orElse("");
        if(cardNum != null && cardNum.trim().length() > 0) {
            Integer number = Integer.parseInt(cardNum.substring(12));
            number = number + 1;
            if(number > 9999) {
                return BaseResponseUtils.buildFail(SellResultCode.CARD_NUMBER_OVERRUN.getMessage());
            }
            cardNum = cardNum.substring(0, 12) + String.format("%04d", number);
        } else {
            cardNum = areaCode + "0001";
        }
 
 
        /**
         * cardAddr         水卡地址(仅仅写入,无业务)
         * clientNum        农户编号
         * cardCost         卡片费用
         * amount           充值金额
         * reissueAmount    补卡金额,补卡时使用
         * paymentId        支付方式编号
         * remarks          备注
         * operator         操作人编号
         * activeTime       开卡时间
         */
        String cardAddr = po.getCardAddr();
        String clientNum = po.getClientNum();
        //Long clientId = po.getClientId();
 
        Integer cardCost = po.getCardCost();
        Float amount = po.getAmount();
        Long paymentId = po.getPaymentId();
        String remarks = po.getRemarks();
        Long operator = po.getOperator();
        Date createTime = new Date();
 
        /**
         * 根据农户编号获取农户ID
         */
        Long clientId = activeCardSv.getClientIdByNum(clientNum);
 
        /**
         * 添加农户卡记录
         */
        SeClientCard seClientCard = new SeClientCard();
        seClientCard.setCardaddr(cardAddr);
        seClientCard.setCardnum(cardNum);
        //seClientCard.setClientnum(clientNum);
        seClientCard.setClientid(clientId);
        seClientCard.setMoney(0f);
        seClientCard.setState(CardStateENUM.NORMAL.getCode());
        seClientCard.setCreatedt(createTime);
        seClientCard.setLastoper(LastOperateENUM.ACTIVE.getCode());
 
        Long cardId = Optional.ofNullable(clientCardSv.add(seClientCard)).orElse(0L) ;
        if(cardId == 0) {
            return BaseResponseUtils.buildFail(SellResultCode.ACTIVE_FAIL_WRITE_CLIENT_CARD_ERROR.getMessage());
        }
 
        /**
         * 添加开卡记录
         */
        SeActiveCard activeCard = new SeActiveCard();
        //activeCard.setCardnum(cardNum);
        //activeCard.setClientnum(clientNum);
 
        activeCard.setCardid(cardId);
        activeCard.setClientid(clientId);
        activeCard.setCardcost(cardCost);
        activeCard.setPaymentid(paymentId);
        activeCard.setOperatetype(OperateTypeENUM.ACTIVE.getCode());
        activeCard.setRemarks(remarks);
        activeCard.setOperator(operator);
        activeCard.setOperatedt(createTime);
        Long rec = Optional.ofNullable(activeCardSv.add(activeCard)).orElse(0L);
        if(rec == 0) {
            return BaseResponseUtils.buildFail(SellResultCode.ACTIVE_FAIL_WRITE_ACTIVE_CARD_ERROR.getMessage());
        }
 
        /**
         * 如果操作人员开卡时输入了充值金额,则开卡后调用充值功能
         */
        po.setCardNum(cardNum);
        if(amount != null && amount > 0) {
            po.setClientId(clientId);
            DtoRecharge dtoRecharge = RechargeDtoMapper.INSTANCT.po2vo(po);
            dtoRecharge.setMoney(0f);
            dtoRecharge.setGift(0f);
            dtoRecharge.setPrice(0f);
            BaseResponse<Boolean> job = rechargeCtrl.addRecharge(dtoRecharge);
            if(!job.getCode().equals("0001")) {
                return BaseResponseUtils.buildFail(SellResultCode.ACTIVE_FAIL_RECHARGE_EXCEPTION.getMessage());
            }
        }
 
        Map map = new HashMap();
        map.put("projectCode", projectCode);
        map.put("cardNum", cardNum);
        return BaseResponseUtils.buildSuccess(map) ;
    }
 
    @Operation(summary = "补卡", description = "补卡")
    @ApiResponses(value = {
            @ApiResponse(
                    responseCode = ResultCodeMsg.RsCode.SUCCESS_CODE,
                    description = "操作结果:true:成功,false:失败(BaseResponse.content)",
                    content = {@Content(mediaType = MediaType.APPLICATION_JSON_VALUE,
                            schema = @Schema(implementation = Boolean.class))}
            )
    })
    @PostMapping(path = "add_reissue", consumes = MediaType.APPLICATION_JSON_VALUE)
    @Transactional(rollbackFor = Exception.class)
    @SsoAop()
    public BaseResponse<Boolean> add_reissue(@RequestBody @Parameter(description = "form表单json数据", required = true) @Valid DtoActiveCard po, @Parameter(hidden = true) BindingResult bindingResult){
        if(bindingResult != null && bindingResult.hasErrors()){
            return BaseResponseUtils.buildFail(Objects.requireNonNull(bindingResult.getFieldError()).getDefaultMessage());
        }
 
        /**
         * cardId           水卡编号(非传入参数,由cardNum反查,更新水卡表用)
         * cardNum          水卡编号
         //* clientNum        农户编号(非传入参数,由cardNum反查,添加补卡使用)
         * clientId         农户ID(非传入参数,由cardNum反查,添加补卡使用)
         * cardCost         卡片费用
         * reissueAmount    补卡金额,补卡时使用
         * paymentId        支付方式编号
         * remarks          备注
         * operator         操作人编号
         * activeTime       补卡时间
         */
        Long cardId = 0L;
        String cardNum = po.getCardNum();
        //String clientNum = "";
        Long clientId = 0L;
        Integer cardCost = po.getCardCost();
        Double reissueAmount = po.getReissueAmount();
        Long paymentId = po.getPaymentId();
        String remarks = po.getRemarks();
        Long operator = po.getOperator();
        Date replaceTime = new Date();
 
        /**
         * 依据水卡编号获取水卡表主键及农户编号
         */
        Map map = Optional.ofNullable(clientCardSv.getCardIdAndClientNum(cardNum)).orElse(new HashMap());
        if(map == null || map.size() <= 0) {
            return BaseResponseUtils.buildFail(SellResultCode.CARD_NUMBER_ERROR.getMessage());
        }
        cardId = Long.parseLong(map.get("cardId").toString());
        //clientNum = map.get("clientNum").toString();
        clientId = Long.parseLong(map.get("clientId").toString());
 
        /**
         * 修改农户卡信息:
         *      补卡时间
         *      最后操作类型-2
         */
        SeClientCard seClientCard = new SeClientCard();
        seClientCard.setId(cardId);
        seClientCard.setReplacedt(replaceTime);
        seClientCard.setLastoper(LastOperateENUM.REPLACE.getCode());
        Integer rec_updateClientCard = Optional.ofNullable(clientCardSv.UpdateClientCard(seClientCard)).orElse(0);
        if(rec_updateClientCard == 0) {
            return BaseResponseUtils.buildFail(SellResultCode.REPLACE_FAIL_WRITE_CLIENT_CARD_ERROR.getMessage());
        }
 
        /**
         * 添加补卡记录
         */
        SeActiveCard activeCard = new SeActiveCard();
        //activeCard.setCardnum(cardNum);
        //activeCard.setClientnum(clientNum);
        activeCard.setCardid(cardId);
        activeCard.setClientid(clientId);
        activeCard.setCardcost(cardCost);
        activeCard.setPaymentid(paymentId);
        activeCard.setReissueamount(reissueAmount);
        activeCard.setOperatetype(OperateTypeENUM.REPLACE.getCode());
        activeCard.setRemarks(remarks);
        activeCard.setOperator(operator);
        activeCard.setOperatedt(replaceTime);
        Long rec = Optional.ofNullable(activeCardSv.add(activeCard)).orElse(0L);
        if(rec == 0) {
            return BaseResponseUtils.buildFail(SellResultCode.REPLACE_FAIL_WRITE_RECHARGE_ERROR.getMessage());
        }
 
        return BaseResponseUtils.buildSuccess(true) ;
    }
}