package org.springblade.app.controller;

import lombok.RequiredArgsConstructor;
import org.springblade.core.tool.api.R;
import org.springblade.enterprise.entity.Staff;
import org.springblade.enterprise.feign.IStaffClient;
import org.springblade.enterprise.vo.StaffVO;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author lidexi
 * @date 2021/5/17 16:58
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/enterprise/staff")
public class EnterpriseStaffController {

	private final IStaffClient staffClient;

	/**
	 * 园区小程序修改密码接口
	 *
	 * @param loginType 0-员工登录（员工无需修改密码）  1-企业登录  2-管理员登录
	 * @param account   账号
	 * @param oldPassword 旧密码
	 * @param newPassword 新密码
	 * @return
	 */
	@PostMapping(value = "/updatePassword")
	public R updatePassword(@RequestParam Integer loginType, @RequestParam String account, @RequestParam String oldPassword, @RequestParam String newPassword) {
		if(loginType == 1){//企业管理员修改密码
			Staff staff = new Staff();
			staff.setEnterpriseAccount(account);
			staff.setEnterprisePassword(oldPassword);
			StaffVO enterpriseManager = staffClient.detail(staff);
			if(enterpriseManager == null){
				return R.data("旧密码不正确");
			}else {
				enterpriseManager.setEnterprisePassword(newPassword);
				return R.data(staffClient.update(enterpriseManager));
			}
		}else if(loginType == 2){//园区管理员修改密码
			Staff staff = new Staff();
			staff.setAgencyAccount(account);
			staff.setAgencyPassword(oldPassword);
			StaffVO agencyManager = staffClient.detail(staff);
			if(agencyManager == null){
				return R.data("旧密码不正确");
			}else {
				agencyManager.setAgencyPassword(newPassword);
				return R.data(staffClient.update(agencyManager));
			}
		}
		return R.data("登录方式不存在");
	}

}
