package com.taoke.autopay.controller.admin.credit; import com.google.gson.Gson; import com.google.gson.GsonBuilder; import com.google.gson.TypeAdapter; import com.google.gson.stream.JsonReader; import com.google.gson.stream.JsonWriter; import com.taoke.autopay.dao.credit.ExchangeRateMapper; import com.taoke.autopay.entity.credit.ExchangeRate; import com.taoke.autopay.service.credit.ExchangeRateService; import com.taoke.autopay.utils.JsonUtil; import com.taoke.autopay.utils.TimeUtil; 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 javax.annotation.Resource; import java.io.IOException; import java.math.BigDecimal; import java.util.Date; import java.util.List; @Controller @RequestMapping("/admin/api/credit/exchangeRate") public class CreditExchangeRateAdminController { private Gson gson = new GsonBuilder().registerTypeAdapter(Date.class, new TypeAdapter() { @Override public void write(JsonWriter out, Date value) throws IOException { String desc = ""; if (value != null) { // 判断是否是同一天 desc = TimeUtil.getGernalTime(value.getTime(), "yyyy-MM-dd HH:mm:ss"); out.value(desc); } else { out.value(""); } } @Override public Date read(JsonReader in) throws IOException { return new Date(); } }).registerTypeAdapter(BigDecimal.class, new TypeAdapter() { @Override public void write(JsonWriter out, BigDecimal value) throws IOException { if (value != null) { out.value(value.toString()); } else { out.value(""); } } @Override public BigDecimal read(JsonReader in) throws IOException { return new BigDecimal("0.00"); } }).registerTypeAdapter(ExchangeRate.ExchangeRateType.class, new TypeAdapter() { @Override public void write(JsonWriter out, ExchangeRate.ExchangeRateType value) throws IOException { if (value != null) { out.value(value.getDesc()); } else { out.value(""); } } @Override public ExchangeRate.ExchangeRateType read(JsonReader in) throws IOException { return ExchangeRate.ExchangeRateType.GENERAL_EXCHANGE; } }) .create(); @Resource private ExchangeRateService exchangeRateService; /** * 获取积分汇率列表 */ @ResponseBody @RequestMapping("list") public String listExchangeRates(int page, int limit) { List exchangeRates = exchangeRateService.listExchangeRates(ExchangeRateMapper.DaoQuery.builder() .start((long) (page - 1) * limit) .count(limit) .build()); long count = exchangeRateService.countExchangeRates(ExchangeRateMapper.DaoQuery.builder().build()); JSONObject root = new JSONObject(); root.put("list", gson.toJson(exchangeRates)); root.put("count", count); return JsonUtil.loadTrueResult(root); } /** * 获取积分汇率详情 */ @ResponseBody @RequestMapping("getDetail") public String getDetail(Long id) { ExchangeRate exchangeRate = exchangeRateService.selectById(id); if (exchangeRate == null) { return JsonUtil.loadFalseResult("积分汇率不存在"); } return JsonUtil.loadTrueResult(gson.toJson(exchangeRate)); } /** * 更新积分汇率 */ @ResponseBody @RequestMapping("update") public String updateExchangeRate(Long id, BigDecimal rate) { int success = exchangeRateService.updateExchangeRate(ExchangeRate.builder() .id(id) .rate(rate) .build()); if (success > 0) { return JsonUtil.loadTrueResult("更新成功"); } else { return JsonUtil.loadFalseResult("更新失败"); } } }