package com.sifang.modules.sunshine.SystemSetting.controller;

import java.math.BigDecimal;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jfinal.plugin.activerecord.Record;
import com.sifang.modules.sunshine.SystemSetting.service.CrmOrderMileageSettingsService;
import com.sifang.modules.sunshine.SystemSetting.utils.Result;
import com.sifang.modules.sys.web.BaseController;

/*
 * 服务商、用户里程结算设置，根据数据库区分字段 type: 1.用户 2.服务商
 * 
 */
@Controller
@RequestMapping(value = "${adminPath}/yangguang/orderMileageSet")
public class CrmOrderMileageSettingsController extends BaseController {
	@Autowired
	private CrmOrderMileageSettingsService crmOrderMileageSettingsService;

	// 里程结算设置-列表页面
	@RequestMapping(value = "orderSiteMileageSetPage")
	public String orderSiteMileageSetPage(HttpServletRequest request, Model model) {
		// 获取服务商里程结算设置记录
		Record rd = crmOrderMileageSettingsService.getSiteMeleageSet("2","1");// type=2为服务商
		Record rd2 = crmOrderMileageSettingsService.getSiteMeleageSet("2","2");// type=2为服务商
		if(rd !=null){
			BigDecimal amount = rd != null ? rd.getBigDecimal("amount") : new BigDecimal(0);
			model.addAttribute("initialValue", rd.getBigDecimal("initial_value"));
			// 显示规则：超过后规定里程后的金额，如果等于0，则不显示，即为空字符串，否则正常显示
			model.addAttribute("amount", amount);
		}
		model.addAttribute("rd2", rd2);
		return "modules/yangguang/systemSetting/orderMileageSet/siteMeleageSet";
	}

	// 服务商/用户里程结算设置-保存 type:1 用户 2服务商
	@ResponseBody
	@RequestMapping(value = "save")
	public Result<Void> save(HttpServletRequest request) {
		Map<String,String> map = getParams(request);
		return crmOrderMileageSettingsService.save(map);
	}

	// 用户里程结算设置-页面
	@RequestMapping(value = "orderFactoryMileageSetPage")
	public String orderFactoryMileageSetPage(HttpServletRequest request, Model model) {
		// 获取用户里程结算设置记录
		Record rd = crmOrderMileageSettingsService.getSiteMeleageSet("1","1");// type=1为用户
		Record rd2 = crmOrderMileageSettingsService.getSiteMeleageSet("1","2");// type=1为用户
		if(rd != null){
			BigDecimal amount = rd != null ? rd.getBigDecimal("amount") : new BigDecimal(0);
			model.addAttribute("initialValue", rd.getBigDecimal("initial_value"));
			model.addAttribute("amount", amount);
			// 显示规则：超过后规定里程后的金额，如果等于0，则不显示，即为空字符串，否则正常显示
		}
		model.addAttribute("rd2", rd2);
		return "modules/yangguang/systemSetting/orderMileageSet/userMeleageSet";
	}


}
