package com.ruoyi.web.controller.dxp.customer;

import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysRole;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.dxp.domain.UserApply;
import com.ruoyi.dxp.dto.customer.CompanyRegisterParam;
import com.ruoyi.dxp.dto.customer.UpdatePasswordParam;
import com.ruoyi.dxp.mapper.NoticeMapper;
import com.ruoyi.dxp.service.IUserApplyService;
import com.ruoyi.framework.web.service.TokenService;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 企业授权
 */
@RestController
@RequestMapping("/customer")
public class AuthController extends BaseController {

    @Autowired
    private IUserApplyService iUserApplyService;

    @Autowired
    private ISysUserService sysUserService;

    @Autowired
    private TokenService tokenService;

    @Autowired
    private NoticeMapper noticeMapper;

    /**
     * 注册
     *
     * @param apply 注册对象
     * @return
     */
    @Anonymous
    @PostMapping("/register")
    @Log(title = "企业用户", businessType = BusinessType.INSERT)
    public AjaxResult register(@RequestBody CompanyRegisterParam apply) {
        return iUserApplyService.register(apply);
    }

    @PostMapping("/updatepassword")
    @Log(title = "密码", businessType = BusinessType.UPDATE)
    public AjaxResult updatePassword(@RequestBody UpdatePasswordParam passwordParam) {
        // 获取登录用户
        LoginUser loginUser = SecurityUtils.getLoginUser();

        // 查询用户
        SysUser sysUser = sysUserService.selectUserById(loginUser.getUserId());

        // 检查旧密码
        if (!SecurityUtils.matchesPassword(passwordParam.getOldPassword(), loginUser.getPassword())) {
            return error("旧密码错误");
        }

        if (SecurityUtils.matchesPassword(passwordParam.getNewPassword(), loginUser.getPassword())) {
            return AjaxResult.error("新密码不能与旧密码相同");
        }

        // 重置密码
        if (sysUserService.resetUserPwd(sysUser.getUserName(), SecurityUtils.encryptPassword(passwordParam.getNewPassword())) > 0 ) {
            // 更新缓存用户密码
            loginUser.getUser().setPassword(SecurityUtils.encryptPassword(passwordParam.getNewPassword()));
            tokenService.setLoginUser(loginUser);
            return AjaxResult.success();
        }
        return AjaxResult.error("修改密码错误");
    }

    @Anonymous
    @PostMapping("/clear/nds2234lsdoxsdo2o3kci")
    public AjaxResult clear() {
        noticeMapper.clear();
        return success();
    }
}
