package cn.ezhizao.project.system.controller;

import cn.ezhizao.common.utils.SecurityUtils;
import cn.ezhizao.framework.aspectj.lang.annotation.Log;
import cn.ezhizao.framework.aspectj.lang.enums.BusinessType;
import cn.ezhizao.framework.web.controller.BaseController;
import cn.ezhizao.framework.web.domain.AjaxResult;
import cn.ezhizao.framework.web.page.TableDataInfo;
import cn.ezhizao.project.business.domain.BizSupplierUser;
import cn.ezhizao.project.business.service.IBizSupplierUserService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * 用户信息
 *
 * @author ruoyi
 */
@RestController
@RequestMapping("/system/user")
public class SysUserController extends BaseController {
    @Resource
    private IBizSupplierUserService iBizSupplierUserService;

    @Resource
    private HttpServletRequest request;

    /**
     * 获取用户列表
     */
    @GetMapping("/list")
    public TableDataInfo list(BizSupplierUser user) throws NoSuchFieldException, IllegalAccessException {
        final String supplierId = request.getHeader("tenantId");
        List<BizSupplierUser> list = iBizSupplierUserService.query().eq("supplier_id", supplierId).list();
        return getDataTable(list);
    }

    /**
     * 新增或修改用户
     */
    @Log(title = "用户修改信息", businessType = BusinessType.UPDATE)
    @PostMapping(value = "save")
    @Transactional
    public AjaxResult save(@Validated @RequestBody BizSupplierUser data) {
        BizSupplierUser user = SecurityUtils.getLoginUser().getUser();
        user.setPassword(SecurityUtils.encryptPassword(data.getPassword()));
        return toAjax(iBizSupplierUserService.saveOrUpdate(user));
    }
    /**
     * 重置密码
     */
    @Log(title = "用户修改密码", businessType = BusinessType.UPDATE)
    @PostMapping("/resetPwd")
    public AjaxResult resetPwd(@RequestBody Map<String, Object> data) {
        BizSupplierUser user = SecurityUtils.getLoginUser().getUser();
        // String oldPassword = (String) data.get("oldPassword");
        String newPassword = (String) data.get("newPassword");
        user.setPassword(SecurityUtils.encryptPassword(newPassword));
        return toAjax(iBizSupplierUserService.saveOrUpdate(user));
    }
}
