cloudroam
2024-12-06 4ecc52d0f6558cbbcec1c86647c9dba04ac66b7d
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
package com.mzl.flower.service.flower;
 
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mzl.flower.config.security.SecurityUtils;
import com.mzl.flower.dto.request.flower.*;
import com.mzl.flower.dto.response.flower.FlowerMarkupSpDTO;
import com.mzl.flower.dto.response.flower.FlowerMarkupSpListDTO;
import com.mzl.flower.entity.flower.FlowerMarkupSp;
import com.mzl.flower.entity.log.OperationRecord;
import com.mzl.flower.entity.partner.Partner;
import com.mzl.flower.mapper.flower.FlowerMarkupSpMapper;
import com.mzl.flower.mapper.log.OperationRecordMapper;
import com.mzl.flower.service.BaseService;
import com.mzl.flower.utils.IpUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.List;
 
@Slf4j
@Service
@Transactional
public class FlowerMarkupSpService extends BaseService {
 
    @Autowired
    private FlowerMarkupSpMapper spMapper;
 
    @Autowired
    private OperationRecordMapper operationRecordMapper;
 
    public void saveMarkupSp(FlowerMarkupSpSaveDTO dto, HttpServletRequest request) throws IOException {
        FlowerMarkupSp sp = spMapper.selectOne(new QueryWrapper<FlowerMarkupSp>()
                .eq("partner_id", dto.getPartnerId()).eq("flower_id", dto.getFlowerId()));
        String content = "";
        OperationRecord operationRecord = new OperationRecord();
        if(sp == null){
            sp = new FlowerMarkupSp();
            sp.setFlowerId(dto.getFlowerId());
            sp.setFee(dto.getFee());
            sp.setPartnerId(dto.getPartnerId());
            sp.create(SecurityUtils.getUserId());
            spMapper.insert(sp);
            content = "新增合伙人加价:商品id:【" + dto.getFlowerId() + "】,合伙人id:【"+dto.getPartnerId()+"】,金额:【" + dto.getFee() + "】";
            operationRecord.setFunction("新增合伙人加价");
        } else {
            sp.setFee(dto.getFee());
            sp.update(SecurityUtils.getUserId());
            spMapper.updateById(sp);
            content = "修改合伙人加价:id:【"+sp.getId()+"】,商品id:【" + dto.getFlowerId() + "】,合伙人id:【"+dto.getPartnerId()+"】,金额:【" + dto.getFee() + "】";
            operationRecord.setFunction("修改合伙人加价");
        }
 
        markupCacheClient.addMarkupSp(dto.getPartnerId(), dto.getFlowerId(), sp);
        operationRecord.create(SecurityUtils.getUserId());
        operationRecord.setStatus("success");
        operationRecord.setModule("markup_s_p");
 
        operationRecord.setContent(content);
        operationRecord.setIpAddress(IpUtil.getIpAddress(request));
        operationRecordMapper.insert(operationRecord);
    }
 
    public void saveMarkupSpBatch(FlowerMarkupSpSaveBatchDTO dto,HttpServletRequest request) throws IOException {
        List<Long> flowerIds = dto.getFlowerIds();
        if(flowerIds != null && flowerIds.size() > 0){
            for(Long flowerId : flowerIds){
                FlowerMarkupSpSaveDTO dt = new FlowerMarkupSpSaveDTO();
                dt.setFlowerId(flowerId);
                dt.setPartnerId(dto.getPartnerId());
                dt.setFee(dto.getFee());
 
                saveMarkupSp(dt,request);
            }
        }
    }
 
    public FlowerMarkupSpDTO getMarkupSp(Long flowerId){
        FlowerMarkupSpDTO dto = new FlowerMarkupSpDTO();
        dto.setFlowerId(flowerId);
 
        Partner s = getCurrentPartner();
        Long sId = s.getId();
        FlowerMarkupSp sp = spMapper.selectOne(new QueryWrapper<FlowerMarkupSp>()
                .eq("partner_id", sId).eq("flower_id", flowerId));
 
        dto.setFee(sp != null ? sp.getFee() : null);
 
        return dto;
    }
 
    public Page<FlowerMarkupSpListDTO> selectMarkupSpFlowerList(Page page, FlowerMarkupSpQueryDTO dto){
        List<FlowerMarkupSpListDTO> ll = spMapper.selectMarkupSpFlowerList(page, dto.getPartnerId(), dto);
        if(ll != null && ll.size() > 0){
            for(FlowerMarkupSpListDTO ss : ll){
                ss.setPrice(getFinalPriceForPartner(dto.getPartnerId(), ss.getCategory(), ss.getFlowerId(), ss.getPrice(), ss.getLevel()));
                BigDecimal fee = ss.getFee();
                if (fee == null) {
                    fee = BigDecimal.ZERO;
                }
                ss.setSellPrice(ss.getPrice().add(fee));
            }
        }
 
        page.setRecords(ll);
        return page;
    }
 
    public Page<FlowerMarkupSpListDTO> selectMarkupSpList(Page page, FlowerMarkupSpQueryDTO dto){
 
        List<FlowerMarkupSpListDTO> ll = spMapper.selectMarkupSpList(page, dto.getPartnerId(), dto);
        if(ll != null && ll.size() > 0){
            for(FlowerMarkupSpListDTO ss : ll){
                ss.setPrice(getFinalPriceForPartner(dto.getPartnerId(), ss.getCategory(), ss.getFlowerId(), ss.getPrice(), ss.getLevel()));
                BigDecimal fee = ss.getFee();
                if (fee == null) {
                    fee = BigDecimal.ZERO;
                }
                ss.setSellPrice(ss.getPrice().add(fee));
            }
        }
 
        page.setRecords(ll);
        return page;
    }
 
    public void deleteMarkupSp(Long flowerId){
        Partner s = getCurrentPartner();
        Long sId = s.getId();
        spMapper.delete(new QueryWrapper<FlowerMarkupSp>()
                .eq("partner_id", sId).eq("flower_id", flowerId));
 
        markupCacheClient.deleteMarkupSp(sId, flowerId);
    }
 
}