admin
2022-09-30 ffc56f912da5d6d842142ae4ea1856bc56f8bcb9
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
package com.yeshi.makemoney.app.controller.admin.goldcorn;
 
import com.google.gson.*;
import com.yeshi.makemoney.app.service.inter.goldcorn.GoldCornGetRecordService;
import com.yeshi.makemoney.app.utils.SystemInfoUtil;
import com.yeshi.makemoney.app.utils.goldcorn.GoldCornUtil;
import com.yeshi.makemoney.app.vo.AcceptAdminData;
import com.yeshi.makemoney.app.vo.admin.goldcorn.GoldCornMoneyExchangeRateAdminVO;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.yeshi.utils.JsonUtil;
import org.yeshi.utils.StringUtil;
import org.yeshi.utils.SystemUtil;
import org.yeshi.utils.TimeUtil;
import com.google.gson.reflect.TypeToken;
 
import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.lang.reflect.Type;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Date;
import java.util.List;
 
import com.yeshi.makemoney.app.entity.goldcorn.GoldCornMoneyExchangeRateRecord;
import com.yeshi.makemoney.app.service.inter.goldcorn.GoldCornMoneyExchangeRateRecordService;
import com.yeshi.makemoney.app.service.query.goldcorn.GoldCornMoneyExchangeRateRecordQuery;
import org.yeshi.utils.statistic.BaseStatisticTimeQuery;
import org.yeshi.utils.statistic.StatisticNumberResult;
import org.yeshi.utils.statistic.StatisticTimeSpan;
 
@Controller
@RequestMapping("admin/api/goldcorn/exchangerate")
public class GoldCornMoneyExchangeRateRecordAdminController {
 
    @Resource
    private GoldCornMoneyExchangeRateRecordService goldCornMoneyExchangeRateRecordService;
 
    @Resource
    private GoldCornGetRecordService goldCornGetRecordService;
 
 
    @ResponseBody
    @RequestMapping("list")
    public String list(GoldCornMoneyExchangeRateRecordQuery query, int page, int limit) {
        List<GoldCornMoneyExchangeRateRecord> list = goldCornMoneyExchangeRateRecordService.list(query, page, limit);
        long count = goldCornMoneyExchangeRateRecordService.count(query);
        JSONObject data = new JSONObject();
        Gson gson = new GsonBuilder().registerTypeAdapter(Date.class, new JsonSerializer<Date>() {
 
            @Override
            public JsonElement serialize(Date date, Type type, JsonSerializationContext jsonSerializationContext) {
                return date == null ? new JsonPrimitive("") : new JsonPrimitive(TimeUtil.getGernalTime(date.getTime(), "yyyy.MM.dd HH:mm"));
            }
        }).create();
 
        data.put("list", gson.toJson(list));
        data.put("count", count);
        return JsonUtil.loadTrueResult(data);
 
    }
 
    @ResponseBody
    @RequestMapping("delete")
    public String delete(String ids) {
        Type type = new TypeToken<List<String>>() {
        }.getType();
        List<String> idList = new Gson().fromJson(ids, type);
 
        for (String id : idList) {
            //生效之后无法修改,无法删除
            GoldCornMoneyExchangeRateRecord record = goldCornMoneyExchangeRateRecordService.get(id);
            if (record.getValidateTime().getTime() <= System.currentTimeMillis()) {
                return JsonUtil.loadFalseResult("已经生效的设置无法删除");
            }
        }
        goldCornMoneyExchangeRateRecordService.delete(idList);
        return JsonUtil.loadTrueResult("");
    }
 
    @ResponseBody
    @RequestMapping("add")
    public String add(String day,String money, String validateTime, AcceptAdminData acceptAdminData) {
 
        if (StringUtil.isNullOrEmpty(money)) {
            return JsonUtil.loadFalseResult("金额不能为空");
        }
 
        GoldCornMoneyExchangeRateAdminVO vo = new GoldCornMoneyExchangeRateAdminVO();
        vo.setDay(day);
        vo.setValidateTime(validateTime);
        //查询日期的金币数量
        BaseStatisticTimeQuery timeQuery = new BaseStatisticTimeQuery();
        timeQuery.setStartTime(GoldCornUtil.convertFormatDay(day));
        timeQuery.setEndTime(GoldCornUtil.convertFormatDay(day));
        timeQuery.setTimeSpan(StatisticTimeSpan.day);
        List<StatisticNumberResult> list = goldCornGetRecordService.statistic(acceptAdminData.getSystem(), timeQuery);
        if (list == null || list.size() == 0) {
            return JsonUtil.loadFalseResult("统计出错");
        }
 
        long number = list.get(0).getNumber();
        //0.1的税
        vo.setRate(new BigDecimal(money).multiply(new BigDecimal("0.9")).divide(new BigDecimal(number),7, RoundingMode.FLOOR));
        try {
            GoldCornMoneyExchangeRateRecord bean = vo.toEntity(acceptAdminData.getSystem());
            goldCornMoneyExchangeRateRecordService.add(bean);
            return JsonUtil.loadTrueResult("");
        } catch (Exception e) {
            return JsonUtil.loadFalseResult(e.getMessage());
        }
    }
 
    @ResponseBody
    @RequestMapping("get")
    public String get(String id, HttpSession session) {
        GoldCornMoneyExchangeRateRecord entity = goldCornMoneyExchangeRateRecordService.get(id);
        if (entity != null) {
            return JsonUtil.loadTrueResult(GoldCornMoneyExchangeRateAdminVO.create(entity));
        } else {
            return JsonUtil.loadFalseResult("ID不存在");
        }
    }
 
 
    @ResponseBody
    @RequestMapping("update")
    public String update(GoldCornMoneyExchangeRateAdminVO bean, HttpSession session) {
        if (bean.getId() == null) {
            return JsonUtil.loadFalseResult("ID不能为空");
        }
 
        //生效之后无法修改,无法删除
        GoldCornMoneyExchangeRateRecord record = goldCornMoneyExchangeRateRecordService.get(bean.getId());
        if (record.getValidateTime().getTime() <= System.currentTimeMillis()) {
            return JsonUtil.loadFalseResult("已经生效的设置无法修改");
        }
 
 
        try {
            goldCornMoneyExchangeRateRecordService.update(bean.toEntity(null));
        } catch (Exception e) {
            return JsonUtil.loadFalseResult(e.getMessage());
 
        }
        return JsonUtil.loadTrueResult("");
 
    }
 
 
}