package com.whn.hellospring.controller; import com.whn.hellospring.common.StateMessage; import com.whn.hellospring.common.Status; import com.whn.hellospring.model.OilDO; import com.whn.hellospring.request.DeleteOilRecordRequest; import com.whn.hellospring.request.OilRecordListRequest; import com.whn.hellospring.service.OilService; import com.whn.hellospring.utils.StringUtil; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; import javax.validation.Valid; import java.util.List; @RestController @RequestMapping(value = "/oil") public class OilController { @Autowired OilService service; /** * 平均油耗 * * @return */ @PostMapping(value = "/wear") public Status getOilWear(@RequestBody OilRecordListRequest request) { try { //平均油耗 = 加油总数量(num 数量相加 除第一次) / 区间里程总数(intervalmileage 相加 除第一次)* 100 double numTotal = service.getNumTotal(request.getCustomer_id()); double intervalMileageTotal = service.getIntervalMileageTotal(request.getCustomer_id()); double oilWear = numTotal * 100.0 / intervalMileageTotal; String s = StringUtil.INSTANCE.doubleToString1(oilWear); Status status = new Status(StateMessage.SUCCESS, "success"); status.setData("wear", s); return status; } catch (Exception e) { return new Status(StateMessage.UN_KNOW_REASON); } } /** * 删除记录 */ @PostMapping(value = "/delete") public Status deleteCoffeeById(@RequestBody DeleteOilRecordRequest request) { try { OilDO oilDO = service.deleteRecord(request.getId()); Status status = new Status(StateMessage.SUCCESS, "success"); return status; } catch (Exception e) { return new Status(StateMessage.UN_KNOW_REASON); } } /** * 获取油耗列表 */ @PostMapping(value = "/wearList") public Status getWearList(@RequestBody OilRecordListRequest request) { try { List wearList = service.getWearList(request.getCustomer_id()); Status status = new Status(StateMessage.SUCCESS, "success"); status.setData("list", wearList); return status; } catch (Exception e) { return new Status(StateMessage.UN_KNOW_REASON); } } /** * 获取记录列表 */ @PostMapping(value = "/recordList") public Status getRecordList(@RequestBody OilRecordListRequest request) { try { List recordList = service.getRecordList(request.getCustomer_id()); Status status = new Status(StateMessage.SUCCESS, "success"); status.setData("list", recordList); return status; } catch (Exception e) { return new Status(StateMessage.UN_KNOW_REASON); } } /** * 创建记录 */ @PostMapping(value = "/add") public Status createRecord(@Valid @RequestBody OilDO oilDO) { try { //上条有效记录的currentMileage OilDO recentlyRecord = service.getRecentlyRecord(oilDO.getCustomer_id()); Double recentlyMileage = 0.0; if (recentlyRecord != null&& recentlyRecord.getMileage()!=null) { recentlyMileage = recentlyRecord.getMileage(); //区间里程intervalmileage = currentMileage - 上条有效记录的currentMileage double intervalMileage = oilDO.getMileage() - recentlyMileage; oilDO.setIntervalMileage(intervalMileage); //计算区间油耗 intervalOilWear = num / intervalmileage double intervalOilWear = oilDO.getNum() * 100.0 / intervalMileage; oilDO.setIntervalOilWear(intervalOilWear); }else{ oilDO.setIntervalOilWear(0.0); } oilDO.setStatus(1); oilDO.setTime(oilDO.getTime()); OilDO oil = service.addUpdateRecord(oilDO); Status status = new Status(StateMessage.SUCCESS, "success"); status.setData("bean", oil); return status; } catch (Exception e) { return new Status(StateMessage.UN_KNOW_REASON); } } // /** // * 更新记录 // */ // @PostMapping(value = "/update") // public Status updateRecord(@Valid @RequestBody OilDO oilDO){ // try { // oilDO.setTime(oilDO.getTime()); // OilDO oil = service.addUpdateRecord(oilDO); // Status status = new Status(StateMessage.SUCCESS, "success"); // status.setData("bean",oil); // return status; // }catch (Exception e){ // return new Status(StateMessage.UN_KNOW_REASON); // } // } }