package quant.rich.emoney.controller;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import quant.rich.emoney.controller.common.BaseController;
import quant.rich.emoney.entity.config.PlatformConfig;
import quant.rich.emoney.exception.RException;
import quant.rich.emoney.pojo.dto.R;
import quant.rich.emoney.service.ConfigService;

@Controller
@RequestMapping("/admin/v1")
public class IndexControllerV1 extends BaseController {
    
    @Autowired
    PlatformConfig platformConfig;
    
    @Autowired
    ConfigService configService;

    @GetMapping({ "", "/", "/index" })
    public String index() {
        return "admin/v1/index";
    }
    
    @PostMapping("/changeUserInfo")
    @ResponseBody
    public R<?> changeUserInfo(String newUsername, String oldPassword, String newPassword) {
        if (!platformConfig.getPassword().equals(oldPassword)) {
            throw RException.badRequest("密码错误");
        }
        if (StringUtils.isAllEmpty(newUsername, newPassword)) {
            throw RException.badRequest("未更改任何信息");
        }
        if (StringUtils.isNotEmpty(newUsername)) {
            platformConfig.setUsername(newUsername);
        }
        if (StringUtils.isNotEmpty(newPassword)) {
            platformConfig.setPassword(newPassword);
        }
        configService.saveOrUpdate(platformConfig);
        return R.ok();
    }

}
