package cn.palmte.work.controller.backend;

import cn.palmte.work.bean.ResponseMsg;
import cn.palmte.work.model.Project;
import cn.palmte.work.model.SysConfig;
import cn.palmte.work.model.SysConfigRepository;
import cn.palmte.work.pojo.SysConfigRequest;
import cn.palmte.work.service.SysConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Map;


@Controller
@RequestMapping("/sys/config")
public class SysConfigController extends BaseController{

    @Autowired
    private SysConfigService sysConfigService;

    @Autowired
    private SysConfigRepository sysConfigRepository;

    @RequestMapping("/edit")
    public String edit(Map<String, Object> model) {
        model.put(SysConfig.KEY_UNDERWRITTENTAXRATE,sysConfigRepository.findByCodeEquals(SysConfig.KEY_UNDERWRITTENTAXRATE).getValue());
        model.put(SysConfig.KEY_PROJECTCONTRIBUTIONPROFITRATETHRESHOLD,sysConfigRepository.findByCodeEquals(SysConfig.KEY_PROJECTCONTRIBUTIONPROFITRATETHRESHOLD).getValue());
        return "admin/profit_marfin_config_input";
    }

    @ResponseBody
    @RequestMapping("/save")
    public ResponseMsg save(@RequestBody String json) {
        return sysConfigService.saveOrUpdate(json);
    }

    @RequestMapping("/checkRate")
    @ResponseBody
    public ResponseMsg checkRate() {
        String value = sysConfigRepository.findByCodeEquals(SysConfig.KEY_UNDERWRITTENTAXRATE).getValue();
        double rate = Double.parseDouble(value);
        if (rate <= 0) {
            return ResponseMsg.buildFailedMsg("项目年利率未填写或小于等于0，请联系管理员配置");
        }
        return ResponseMsg.buildSuccessMsg("成功");
    }

}
