package com.hzs.system.sys.controller.manage;

import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.hzs.common.core.annotation.AccessPermissions;
import com.hzs.common.core.annotation.Log;
import com.hzs.common.core.constant.msg.CommonMsgConstants;
import com.hzs.common.core.domain.FileResult;
import com.hzs.common.core.enums.*;
import com.hzs.common.core.utils.DateUtils;
import com.hzs.common.core.utils.UploadFileUtil;
import com.hzs.common.core.utils.poi.ExcelUtil;
import com.hzs.common.domain.member.base.CuMemberTeam;
import com.hzs.common.domain.system.base.BdCountry;
import com.hzs.common.domain.system.sys.*;
import com.hzs.common.domain.vertex.BdVertex;
import com.hzs.common.util.TransactionUtils;
import com.hzs.member.base.IMemberTeamServiceApi;
import com.hzs.system.base.service.IBdCountryService;
import com.hzs.system.base.service.IBdCurrencyService;
import com.hzs.system.config.service.IBdVertexService;
import com.hzs.system.sys.param.CenterMemberParam;
import com.hzs.system.sys.param.SysUserParam;
import com.hzs.system.sys.param.SysUserQueryParam;
import com.hzs.system.sys.service.*;
import com.hzs.system.sys.vo.SysUserDetailVO;
import com.hzs.system.sys.vo.SysUserQueryVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.hzs.common.core.constant.UserConstants;
import com.hzs.common.core.utils.StringUtils;
import com.hzs.common.core.web.controller.BaseController;
import com.hzs.common.core.web.domain.AjaxResult;
import com.hzs.common.core.web.page.TableDataInfo;
import com.hzs.common.security.utils.SecurityUtils;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;

/**
 * 用户信息
 *
 * @author hzs
 */
@Slf4j
@RestController
@RequestMapping("/manage/user")
public class SysUserController extends BaseController {

    @Autowired
    private ISysUserService iSysUserService;
    @Autowired
    private ISysRoleService iSysRoleService;
    @Autowired
    private ISysUserCountryService iSysUserCountryService;
    @Autowired
    private ISysUserVertexService iSysUserVertexService;
    @Autowired
    private ISysUserTeamService iSysUserTeamService;
    @Autowired
    private IBdCountryService iBdCountryService;
    @Autowired
    private IBdVertexService iBdVertexService;
    @Autowired
    private IBdCurrencyService iBdCurrencyService;
    @Autowired
    private ISysPermissionService iSysPermissionService;

    @DubboReference
    IMemberTeamServiceApi iMemberTeamServiceApi;

    /**
     * 管理员列表
     *
     * @param param
     * @return
     */
    @AccessPermissions("adminRole")
    @Log(module = EOperationModule.ADMIN_CONFIG, business = EOperationBusiness.ADMIN_CONFIG, method = EOperationMethod.SELECT)
    @GetMapping("/list")
    public TableDataInfo list(SysUserQueryParam param) {
        DateUtils.handTimeStartAndEnd(param.getCreationTime());
        param.setUserType(ELoginType.USER.getValueInt());
        startPage();
        List<SysUserQueryVO> list = iSysUserService.selectUserList(param);

        if (CollectionUtil.isNotEmpty(list)) {
            // 结算国家
            Map<Integer, BdCountry> countryMap = iBdCountryService.getAllCountryMap();
            // 查询体系（暂时查询全部，后续如果数据多，再进行优化）
            Map<Integer, BdVertex> vertexMap = iBdVertexService.getAllVertexMap();
            // 查询团队（暂时查询全部，后续如果数据多，再进行优化）
            Map<Long, CuMemberTeam> teamMap = iMemberTeamServiceApi.getTeamByVertexIds(null).getData();
            // 所属角色
            Map<Long, SysRole> roleMap = iSysRoleService.getAllRoleMap();

            for (SysUserQueryVO sysUserQueryVO : list) {
                // 处理结算国家
                if (StringUtils.isNotEmpty(sysUserQueryVO.getCountryIds())) {
                    StringBuilder countryVal = new StringBuilder();
                    for (String countryId : sysUserQueryVO.getCountryIds().split(",")) {
                        BdCountry bdCountry = countryMap.get(Integer.parseInt(countryId));
                        if (null != bdCountry) {
                            countryVal.append(bdCountry.getShortName()).append("/");
                        }
                    }
                    if (countryVal.length() > 0) {
                        sysUserQueryVO.setCountryVal(countryVal.substring(0, countryVal.length() - 1));
                    } else {
                        sysUserQueryVO.setCountryVal(countryVal.toString());
                    }
                }
                // 处理查询体系
                if (StringUtils.isNotEmpty(sysUserQueryVO.getVertexIds())) {
                    StringBuilder vertexVal = new StringBuilder();
                    for (String vertexId : sysUserQueryVO.getVertexIds().split(",")) {
                        BdVertex bdVertex = vertexMap.get(Integer.parseInt(vertexId));
                        if (null != bdVertex) {
                            vertexVal.append(bdVertex.getVertexName()).append("/");
                        }
                    }
                    if (vertexVal.length() > 0) {
                        sysUserQueryVO.setVertexVal(vertexVal.substring(0, vertexVal.length() - 1));
                    } else {
                        sysUserQueryVO.setVertexVal(vertexVal.toString());
                    }
                }
                // 处理查询团队
                if (StringUtils.isNotEmpty(sysUserQueryVO.getTeamIds())) {
                    StringBuilder teamVal = new StringBuilder();
                    for (String teamId : sysUserQueryVO.getTeamIds().split(",")) {
                        CuMemberTeam cuMemberTeam = teamMap.get(Long.parseLong(teamId));
                        if (null != cuMemberTeam) {
                            teamVal.append(cuMemberTeam.getTeamName()).append("/");
                        }
                    }
                    if (teamVal.length() > 0) {
                        sysUserQueryVO.setTeamVal(teamVal.substring(0, teamVal.length() - 1));
                    } else {
                        sysUserQueryVO.setTeamVal(teamVal.toString());
                    }
                }
                // 查询所属角色
                if (StringUtils.isNotEmpty(sysUserQueryVO.getRoleIds())) {
                    StringBuilder roleVal = new StringBuilder();
                    for (String roleId : sysUserQueryVO.getRoleIds().split(",")) {
                        SysRole sysRole = roleMap.get(Long.parseLong(roleId));
                        if (null != sysRole) {
                            roleVal.append(sysRole.getRoleName()).append("/");
                        }
                    }
                    if (roleVal.length() > 0) {
                        sysUserQueryVO.setRoleVal(roleVal.substring(0, roleVal.length() - 1));
                    } else {
                        sysUserQueryVO.setRoleVal(roleVal.toString());
                    }
                }

                sysUserQueryVO.setSmsVal(EYesNo.getLabelByIntValue(sysUserQueryVO.getSms()));
                sysUserQueryVO.setAuthorityVal(EYesNo.getLabelByIntValue(sysUserQueryVO.getAuthority()));
                sysUserQueryVO.setUsualAuthorityVal(EYesNo.getLabelByIntValue(sysUserQueryVO.getUsualAuthority()));
                sysUserQueryVO.setStatusVal(EnableStatus.getLabelByIntValue(sysUserQueryVO.getStatus()));
            }
        }
        return getDataTable(list);
    }

    /**
     * 管理员导出
     */
    @Log(module = EOperationModule.ADMIN_CONFIG, business = EOperationBusiness.ADMIN_CONFIG, method = EOperationMethod.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysUserQueryParam param) {
        DateUtils.handTimeStartAndEnd(param.getCreationTime());
        param.setUserType(ELoginType.USER.getValueInt());
        List<SysUserQueryVO> list = iSysUserService.selectUserList(param);

        if (CollectionUtil.isNotEmpty(list)) {
            // 结算国家
            Map<Integer, BdCountry> countryMap = iBdCountryService.getAllCountryMap();
            // 查询体系（暂时查询全部，后续如果数据多，再进行优化）
            Map<Integer, BdVertex> vertexMap = iBdVertexService.getAllVertexMap();
            // 查询团队（暂时查询全部，后续如果数据多，再进行优化）
            Map<Long, CuMemberTeam> teamMap = iMemberTeamServiceApi.getTeamByVertexIds(null).getData();
            // 所属角色
            Map<Long, SysRole> roleMap = iSysRoleService.getAllRoleMap();

            for (SysUserQueryVO sysUserQueryVO : list) {
                // 处理结算国家
                if (StringUtils.isNotEmpty(sysUserQueryVO.getCountryIds())) {
                    StringBuilder countryVal = new StringBuilder();
                    for (String countryId : sysUserQueryVO.getCountryIds().split(",")) {
                        BdCountry bdCountry = countryMap.get(Integer.parseInt(countryId));
                        if (null != bdCountry) {
                            countryVal.append(bdCountry.getShortName()).append("/");
                        }
                    }
                    if (countryVal.length() > 0) {
                        sysUserQueryVO.setCountryVal(countryVal.substring(0, countryVal.length() - 1));
                    } else {
                        sysUserQueryVO.setCountryVal(countryVal.toString());
                    }
                }
                // 处理查询体系
                if (StringUtils.isNotEmpty(sysUserQueryVO.getVertexIds())) {
                    StringBuilder vertexVal = new StringBuilder();
                    for (String vertexId : sysUserQueryVO.getVertexIds().split(",")) {
                        BdVertex bdVertex = vertexMap.get(Integer.parseInt(vertexId));
                        if (null != bdVertex) {
                            vertexVal.append(bdVertex.getVertexName()).append("/");
                        }
                    }
                    if (vertexVal.length() > 0) {
                        sysUserQueryVO.setVertexVal(vertexVal.substring(0, vertexVal.length() - 1));
                    } else {
                        sysUserQueryVO.setVertexVal(vertexVal.toString());
                    }
                }
                // 处理查询团队
                if (StringUtils.isNotEmpty(sysUserQueryVO.getTeamIds())) {
                    StringBuilder teamVal = new StringBuilder();
                    for (String teamId : sysUserQueryVO.getTeamIds().split(",")) {
                        CuMemberTeam cuMemberTeam = teamMap.get(Long.parseLong(teamId));
                        if (null != cuMemberTeam) {
                            teamVal.append(cuMemberTeam.getTeamName()).append("/");
                        }
                    }
                    if (teamVal.length() > 0) {
                        sysUserQueryVO.setTeamVal(teamVal.substring(0, teamVal.length() - 1));
                    } else {
                        sysUserQueryVO.setTeamVal(teamVal.toString());
                    }
                }
                // 查询所属角色
                if (StringUtils.isNotEmpty(sysUserQueryVO.getRoleIds())) {
                    StringBuilder roleVal = new StringBuilder();
                    for (String roleId : sysUserQueryVO.getRoleIds().split(",")) {
                        SysRole sysRole = roleMap.get(Long.parseLong(roleId));
                        if (null != sysRole) {
                            roleVal.append(sysRole.getRoleName()).append("/");
                        }
                    }
                    if (roleVal.length() > 0) {
                        sysUserQueryVO.setRoleVal(roleVal.substring(0, roleVal.length() - 1));
                    } else {
                        sysUserQueryVO.setRoleVal(roleVal.toString());
                    }
                }
            }
        }

        ExcelUtil<SysUserQueryVO> util = new ExcelUtil<>(SysUserQueryVO.class);
        util.exportExcel(response, list, "管理员导出");
    }

    /**
     * 管理员信息
     */
    @GetMapping(value = {"/{userId}"})
    public AjaxResult getInfo(@PathVariable(value = "userId") Long userId) {
        SysUserDetailVO sysUserDetailVO = new SysUserDetailVO();

        SysUser sysUser = iSysUserService.selectUserById(userId);
        if (null != sysUser) {
            BeanUtil.copyProperties(sysUser, sysUserDetailVO);
            // 所属角色
            sysUserDetailVO.setRoleIdList(sysUser.getRoleList().stream().map(SysRole::getRoleId).collect(Collectors.toList()));
            // 结算国家
            LambdaQueryWrapper<SysUserCountry> countryQueryWrapper = new LambdaQueryWrapper<>();
            countryQueryWrapper.eq(SysUserCountry::getUserId, userId);
            countryQueryWrapper.orderByAsc(SysUserCountry::getIsDefault, SysUserCountry::getPkCountry);
            List<SysUserCountry> sysUserCountryList = iSysUserCountryService.list(countryQueryWrapper);
            if (CollectionUtil.isNotEmpty(sysUserCountryList)) {
                sysUserDetailVO.setCountryIdList(sysUserCountryList.stream().map(SysUserCountry::getPkCountry).collect(Collectors.toList()));
            }
            // 查询体系
            LambdaQueryWrapper<SysUserVertex> vertexQueryWrapper = new LambdaQueryWrapper<>();
            vertexQueryWrapper.eq(SysUserVertex::getUserId, userId);
            vertexQueryWrapper.orderByAsc(SysUserVertex::getPkVertex);
            List<SysUserVertex> sysUserVertexList = iSysUserVertexService.list(vertexQueryWrapper);
            if (CollectionUtil.isNotEmpty(sysUserVertexList)) {
                sysUserDetailVO.setVertexIdList(sysUserVertexList.stream().map(SysUserVertex::getPkVertex).collect(Collectors.toList()));
            }
            // 查询团队
            LambdaQueryWrapper<SysUserTeam> teamQueryWrapper = new LambdaQueryWrapper<>();
            teamQueryWrapper.eq(SysUserTeam::getUserId, userId);
            teamQueryWrapper.orderByAsc(SysUserTeam::getPkTeam);
            List<SysUserTeam> sysUserTeamList = iSysUserTeamService.list(teamQueryWrapper);
            if (CollectionUtil.isNotEmpty(sysUserTeamList)) {
                sysUserDetailVO.setTeamIdList(sysUserTeamList.stream().map(SysUserTeam::getPkTeam).collect(Collectors.toList()));
            }
        }
        return AjaxResult.success(sysUserDetailVO);
    }

    /**
     * 新增管理员
     *
     * @param param
     * @return
     */
    @Log(module = EOperationModule.ADMIN_CONFIG, business = EOperationBusiness.ADMIN_CONFIG, method = EOperationMethod.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysUserParam param) {
        if (UserConstants.NOT_UNIQUE.equals(iSysUserService.checkUserNameUnique(param.getUserName()))) {
            return AjaxResult.error("新增管理员'" + param.getUserName() + "'失败，管理编号已存在");
        } else if (StringUtils.isNotEmpty(param.getPhonenumber())
                && UserConstants.NOT_UNIQUE.equals(iSysUserService.checkPhoneUnique(param.getPhonenumber(), param.getUserId()))) {
            return AjaxResult.error("新增管理员'" + param.getUserName() + "'失败，联系方式已存在");
        }
        param.setPassword(SecurityUtils.encryptPassword(param.getPassword()));
        param.setUserType(ELoginType.USER.getValueInt());
        return toAjax(iSysUserService.insertUser(param, SecurityUtils.getUserId(), SecurityUtils.getPkCountry()));
    }

    /**
     * 修改管理员
     *
     * @param param
     * @return
     */
    @Log(module = EOperationModule.ADMIN_CONFIG, business = EOperationBusiness.ADMIN_CONFIG, method = EOperationMethod.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysUserParam param) {
        if (null == param.getUserId()) {
            return AjaxResult.error(TransactionUtils.getContent(CommonMsgConstants.MISSING_PARAM));
        }
//        iSysUserService.checkUserAllowed(param.getUserId());
        if (StringUtils.isNotEmpty(param.getPhonenumber())
                && UserConstants.NOT_UNIQUE.equals(iSysUserService.checkPhoneUnique(param.getPhonenumber(), param.getUserId()))) {
            return AjaxResult.error("修改管理员'" + param.getUserName() + "'失败，联系方式已存在");
        }
        // 同步2024.01.22需求修改，审批流程同部门用户可以出现重复，此处可以不再做校验
//        Integer countItem = iSysApprovalItemService.itemExistByUserAndDept(param.getUserId(), param.getPkDept());
//        if (null != countItem && countItem > 0) {
//            return AjaxResult.error("管理员所在部门审批配置中出现重复");
//        }
//        Integer countDetail = iSysApprovalBusinessDetailService.detailExistByUserAndDept(param.getUserId(), param.getPkDept());
//        if (null != countDetail && countDetail > 0) {
//            return AjaxResult.error("管理员所在部门存在正在审批中的签呈");
//        }

        if (StringUtils.isNotEmpty(param.getPassword())) {
            param.setPassword(SecurityUtils.encryptPassword(param.getPassword()));
        }
        param.setUserType(ELoginType.USER.getValueInt());

        return toAjax(iSysUserService.updateUser(param, SecurityUtils.getUserId()));
    }

    /**
     * 删除用户
     */
    @Log(module = EOperationModule.ADMIN_CONFIG, business = EOperationBusiness.ADMIN_CONFIG, method = EOperationMethod.DELETE)
    @DeleteMapping("/{userIds}")
    public AjaxResult remove(@PathVariable Long[] userIds) {
        if (ArrayUtils.contains(userIds, SecurityUtils.getUserId())) {
            return AjaxResult.error("当前用户不能删除");
        }
        for (Long userId : userIds) {
            iSysUserService.checkUserAllowed(userId);
        }
        return toAjax(iSysUserService.deleteUserByIds(userIds, SecurityUtils.getUserId()));
    }

    /**
     * 状态修改
     *
     * @param user
     * @return
     */
    @Log(module = EOperationModule.ADMIN_CONFIG, business = EOperationBusiness.ADMIN_CONFIG, method = EOperationMethod.UPDATE_STATUS)
    @PutMapping("/change-status")
    public AjaxResult changeStatus(@RequestBody SysUser user) {
        if (null == user.getUserId()) {
            return AjaxResult.error(TransactionUtils.getContent(CommonMsgConstants.MISSING_PARAM));
        }
        iSysUserService.checkUserAllowed(user.getUserId());
        user.setPkModified(SecurityUtils.getUserId());
        return toAjax(iSysUserService.updateUserStatus(user));
    }

    /**
     * 角色下管理员
     *
     * @param roleId
     * @return
     */
    @GetMapping("/role-user/{roleId}")
    public AjaxResult roleUser(@PathVariable("roleId") Long roleId) {
        return AjaxResult.success(iSysUserService.queryUserByRoleId(roleId));
    }


    /**
     * 可审批管理员
     *
     * @return
     */
    @GetMapping("/approval-user")
    public AjaxResult approvalUser() {
        SysUserQueryParam param = new SysUserQueryParam();
        param.setAuthority(EYesNo.YES.getIntValue());
        param.setPkCountry(SecurityUtils.getPkCountry());
        param.setUserType(ELoginType.USER.getValueInt());
        return AjaxResult.success(iSysUserService.selectUserList(param));
    }

    /**
     * 可日常审批管理员
     *
     * @return
     */
    @GetMapping("/usual-approval-user")
    public AjaxResult usualApprovalUser() {
        SysUserQueryParam param = new SysUserQueryParam();
        param.setUsualAuthority(EYesNo.YES.getIntValue());
        param.setPkCountry(SecurityUtils.getPkCountry());
        param.setUserType(ELoginType.USER.getValueInt());
        return AjaxResult.success(iSysUserService.selectUserList(param));
    }

    /**
     * 全部管理员
     *
     * @return
     */
    @GetMapping("/all")
    public AjaxResult all() {
        List<SysUser> list = iSysUserService.selectAll();
        list.parallelStream().forEach(sysUser -> {
            sysUser.setRoleIdList(sysUser.getRoleList().stream().map(SysRole::getRoleId).collect(Collectors.toList()));
        });
        return AjaxResult.success(list);
    }

    /**
     * 获取登录管理员信息
     *
     * @return
     */
    @GetMapping("/get-info")
    public AjaxResult getInfo() {
        Long userId = SecurityUtils.getUserId();
        Integer pkCountry = SecurityUtils.getPkCountry();

        AjaxResult ajaxResult = AjaxResult.success();

        // 用户
        SysUser sysUser = iSysUserService.selectUserById(userId);
        sysUser.setPkCountry(pkCountry);

        if (ELoginType.USER.getValueInt().equals(sysUser.getUserType())) {
            // 系统管理员
            // 按钮权限
            ajaxResult.put("buttons", iSysPermissionService.getButtonPermission(userId));
            // 国家
            ajaxResult.put("country", iBdCountryService.getCountry(pkCountry));
            // 币种
            ajaxResult.put("currency", iBdCurrencyService.getCurrency(pkCountry));
        }
        ajaxResult.put("user", sysUser);

        if (ELoginType.USER.getValueInt().equals(sysUser.getUserType())) {
            // 系统管理员
            // 按钮权限
            ajaxResult.put("buttons", iSysPermissionService.getButtonPermission(userId));
            // 国家
            ajaxResult.put("country", iBdCountryService.getCountry(pkCountry));
            // 币种
            ajaxResult.put("currency", iBdCurrencyService.getCurrency(pkCountry));
        }

        return ajaxResult;
    }

    /**
     * 个人中心
     *
     * @return
     */
    @Log(module = EOperationModule.HOME_PAGE, business = EOperationBusiness.SYS_INFO, method = EOperationMethod.SELECT)
    @GetMapping("/center")
    public AjaxResult center() {
        // 会员ID
        Long userId = SecurityUtils.getUserId();
        // 用户
        SysUser sysUser = iSysUserService.selectUserById(SecurityUtils.getUserId());
        if (ELoginType.USER.getValueInt().equals(sysUser.getUserType())) {
            // 系统管理员
            // 结算国家
            sysUser.setCountryList(iBdCountryService.queryListByUserId(userId));
            // 查询体系
            sysUser.setVertexList(iBdVertexService.queryListByUserId(userId));
            // 查询团队
            sysUser.setTeamList(iSysUserTeamService.queryListByUserId(userId));
        }

        return AjaxResult.success(sysUser);
    }

    /**
     * 修改基础资料
     *
     * @param param
     * @return
     */
    @Log(module = EOperationModule.HOME_PAGE, business = EOperationBusiness.SYS_INFO, method = EOperationMethod.UPDATE, remark = "修改信息")
    @PutMapping("/center-base")
    public AjaxResult centerBase(@RequestBody CenterMemberParam param) {
        if (StringUtils.isAnyEmpty(param.getNickName(), param.getPhone())) {
            return AjaxResult.error(TransactionUtils.getContent(CommonMsgConstants.MISSING_PARAM));
        }

        SysUser updateUser = new SysUser();
        updateUser.setUserId(SecurityUtils.getUserId());
        updateUser.setNickName(param.getNickName());
        updateUser.setPhonenumber(param.getPhone());
        updateUser.setPkModified(SecurityUtils.getUserId());
        iSysUserService.updateUserProfile(updateUser);

        return AjaxResult.success();
    }

    /**
     * 修改密码
     *
     * @param param
     * @return
     */
    @Log(module = EOperationModule.HOME_PAGE, business = EOperationBusiness.SYS_INFO, method = EOperationMethod.UPDATE, remark = "修改密码")
    @PutMapping("/center-pwd")
    public AjaxResult centerPwd(@RequestBody CenterMemberParam param) {
        if (StringUtils.isAnyEmpty(param.getOldPassword(), param.getNewPassword())) {
            return AjaxResult.error(TransactionUtils.getContent(CommonMsgConstants.MISSING_PARAM));
        }

        // 用户
        SysUser sysUser = iSysUserService.selectUserById(SecurityUtils.getUserId());

        if (SecurityUtils.matchesPassword(param.getOldPassword(), sysUser.getPassword())) {
            // 校验旧密码是否正确
            SysUser updateUser = new SysUser();
            updateUser.setUserId(sysUser.getUserId());
            updateUser.setPassword(SecurityUtils.encryptPassword(param.getNewPassword()));
            updateUser.setPkModified(SecurityUtils.getUserId());
            if (ELoginType.USER.getValueInt().equals(sysUser.getUserType())) {
                // 系统管理员
                updateUser.setPwd(param.getNewPassword());
            }
            return toAjax(iSysUserService.resetPwd(updateUser));
        }
        return AjaxResult.error("旧密码不正确");
    }

    /**
     * 更新头像
     *
     * @param file
     * @return
     */
    @Log(module = EOperationModule.HOME_PAGE, business = EOperationBusiness.SYS_INFO, method = EOperationMethod.UPDATE, remark = "修改头像")
    @PostMapping("/center-head")
    public AjaxResult centerHead(MultipartFile file) {
        if (null == file) {
            return AjaxResult.error(TransactionUtils.getContent(CommonMsgConstants.MISSING_PARAM));
        }
        // 用户
        SysUser sysUser = iSysUserService.selectUserById(SecurityUtils.getUserId());

        FileResult fileResult = UploadFileUtil.upload(file, "systemHead");
        if (null != fileResult) {
            // 修改信息
            SysUser updateUser = new SysUser();
            updateUser.setUserId(sysUser.getUserId());
            updateUser.setHeadPath(fileResult.getUrl());
            updateUser.setPkModified(SecurityUtils.getUserId());
            iSysUserService.updateUserProfile(updateUser);

            return AjaxResult.success(fileResult);
        }
        return AjaxResult.error();
    }


}
