package com.fs.hisStore.controller.store;

import com.fs.common.annotation.Log;
import com.fs.common.constant.Constants;
import com.fs.common.core.domain.AjaxResult;
import com.fs.common.core.domain.entity.SysDictData;
import com.fs.common.core.domain.model.LoginBody;
import com.fs.common.enums.BusinessType;
import com.fs.common.utils.StringUtils;
import com.fs.framework.service.LoginServiceScrm;
import com.fs.framework.service.TokenServiceScrm;
import com.fs.hisStore.domain.StoreLoginUserScrm;
import com.fs.hisStore.domain.FsStoreScrm;
import com.fs.hisStore.service.IFsStoreScrmService;
import com.fs.hisStore.utils.UserUtil;
import com.fs.system.service.ISysDictTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * 登录验证
 *

 */
@RestController
@RequestMapping("/store")
public class FsStoreInfoScrmController
{
    @Autowired
    private LoginServiceScrm loginService;



    @Autowired
    private ISysDictTypeService dictTypeService;

    @Autowired
    private TokenServiceScrm tokenService;

    @Autowired
    private IFsStoreScrmService fsStoreService;

    @Autowired
    private PasswordEncoder passwordEncoder;


    /**
     * 登录方法
     *
     * @param loginBody 登录信息
     * @return 结果
     */
    @PostMapping("/login")
    public AjaxResult login(@RequestBody LoginBody loginBody)
    {
        AjaxResult ajax = AjaxResult.success();
        // 生成令牌
        String token = loginService.login(loginBody.getUsername(), loginBody.getPassword(), loginBody.getCode(),
                loginBody.getUuid());
        ajax.put(Constants.TOKEN, token);
        return ajax;
    }

    /**
     * 获取用户信息
     *
     * @return 用户信息
     */
    @GetMapping("getInfo")
    public AjaxResult getInfo()
    {
        FsStoreScrm user = UserUtil.getLoginUser().getFsStore();

        AjaxResult ajax = AjaxResult.success();
        ajax.put("user", user);
        return ajax;
    }



    /**
     * 个人信息
     */
    @GetMapping("/system/user/profile")
    public AjaxResult profile()
    {
        StoreLoginUserScrm loginUser = UserUtil.getLoginUser();
        FsStoreScrm fsStore = fsStoreService.selectFsStoreByStoreId(loginUser.getFsStore().getStoreId());
        AjaxResult ajax = AjaxResult.success(fsStore);
        return ajax;
    }

    /**
     * 重置密码
     */
    @Log(title = "个人信息", businessType = BusinessType.UPDATE)
    @PutMapping("/system/user/profile/updatePwd")
    public AjaxResult updatePwd(String oldPassword, String newPassword)
    {

        StoreLoginUserScrm loginUser = UserUtil.getLoginUser();
        String password = loginUser.getPassword();
        if ( !passwordEncoder.matches(oldPassword, password)){
            return AjaxResult.error("修改密码失败，旧密码错误");
        }

        String encode = passwordEncoder.encode(newPassword);
        FsStoreScrm fsStore = new FsStoreScrm();
        fsStore.setStoreId(loginUser.getFsStore().getStoreId());
        fsStore.setPassword(encode);
        if (fsStoreService.updateFsStore(fsStore) > 0)
        {
            // 更新缓存用户信息
            FsStoreScrm fsStore1 = loginUser.getFsStore();
            fsStore1.setPassword(fsStore.getPassword());
            loginUser.setFsStore(fsStore1);
            tokenService.setLoginUser(loginUser);
            return AjaxResult.success();
        }
        return AjaxResult.error("修改密码异常，请联系管理员");
    }



}
