package com.aiotagro.cattletrade.business.service.impl;

import cn.hutool.crypto.digest.DigestUtil;
import com.aiotagro.cattletrade.business.dto.SysUserDto;
import com.aiotagro.cattletrade.business.entity.SysUser;
import com.aiotagro.cattletrade.business.mapper.SysUserMapper;
import com.aiotagro.cattletrade.business.service.ISysUserService;
import com.aiotagro.common.core.constant.Constants;
import com.aiotagro.common.core.utils.SecurityUtil;
import com.aiotagro.common.core.web.domain.AjaxResult;
import com.aiotagro.common.core.web.domain.PageResultResponse;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import com.aiotagro.common.core.utils.StringUtils;

import java.util.List;

/**
 * <p>
 * 员工管理表 服务实现类
 * </p>
 *
 * @author admin
 * @since 2024-12-26
 */
@Service
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> implements ISysUserService {

    @Autowired(required = false)
    private SysUserMapper sysUserMapper;

    @Override
    public PageResultResponse queryList(SysUserDto dto) {
        Page<SysUser> result = PageHelper.startPage(dto.getPageNum(), dto.getPageSize());
        List<SysUser> list = sysUserMapper.queryList(dto);
        return new PageResultResponse(result.getTotal(), list);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult saveUser(SysUser user) {
        List<SysUser> users = sysUserMapper.selectList(Wrappers.<SysUser>query().lambda().eq(SysUser::getIsDelete, 0).eq(SysUser::getMobile, user.getMobile()));
        // 新增
        if (user.getId() == null) {
            if (!CollectionUtils.isEmpty(users)) {
                return AjaxResult.error("手机号已存在");
            }
            if (StringUtils.isEmpty(user.getPassword())) {
                user.setPassword("123456");
            }
            user.setPassword(DigestUtil.md5Hex(Constants.USER_PASSWORD_PREFIX + user.getPassword()));
            user.setCreateBy(SecurityUtil.getCurrentUserId());
            save(user);
        } else {
            // 编辑
            if (!CollectionUtils.isEmpty(users) && !users.get(0).getId().equals(user.getId())) {
                return AjaxResult.error("手机号已存在");
            }
            SysUser sysUser = getById(user.getId());
            sysUser.setName(user.getName());
            sysUser.setRoleId(user.getRoleId());
            sysUser.setMobile(user.getMobile());
            sysUser.setStatus(user.getStatus());
            // 传了新密码才更新
            if (StringUtils.isNotEmpty(user.getPassword())) {
                sysUser.setPassword(DigestUtil.md5Hex(Constants.USER_PASSWORD_PREFIX + user.getPassword()));
            }
            sysUser.setUpdateBy(SecurityUtil.getCurrentUserId());
            updateById(sysUser);
        }
        return AjaxResult.success();
    }

    @Override
    public AjaxResult delete(Integer id) {
        removeById(id);
        return AjaxResult.success();
    }

}
