package com.mosty.common.core.business.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mosty.common.base.constant.enums.DeletedEnum;
import com.mosty.common.config.entity.vo.EditPositionVO;
import com.mosty.common.core.business.entity.SysPosition;
import com.mosty.common.core.business.entity.SysUser;
import com.mosty.common.core.business.entity.vo.PositionPage;
import com.mosty.common.core.business.mapper.SysPositionMapper;
import com.mosty.common.core.business.mapper.SysUserMapper;
import com.mosty.common.core.business.service.SysPositionService;
import com.mosty.common.core.business.service.SysUserService;
import com.mosty.common.token.UserInfo;
import com.mosty.common.token.UserInfoManager;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * <p>
 * 岗位表 服务实现类
 * </p>
 *
 * @author kevin
 * @since 2022-02-12
 */
@Slf4j
@Service
@AllArgsConstructor
public class SysPositionServiceImpl extends ServiceImpl<SysPositionMapper, SysPosition> implements SysPositionService {

    private final SysUserService sysUserService;
    private final SysPositionMapper sysPositionMapper;

    @Override
    public IPage<SysPosition> selectByPage(PositionPage page) {
        QueryWrapper<SysPosition> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotBlank(page.getPositionName())) {
            queryWrapper.and(wrapper -> wrapper.like("post_name", page.getPositionName()));
        }
        if (StringUtils.isNotBlank(page.getPositionCode())) {
            queryWrapper.and(wrapper -> wrapper.eq("post_code", page.getPositionCode()));
        }
        queryWrapper.and(wrapper -> wrapper.eq("xt_zxbz", DeletedEnum.NATURE.code));
        queryWrapper.orderByDesc("xt_zhxgsj");
        return getBaseMapper().selectPage(page, queryWrapper);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateSysPost(SysPosition oldSysPosition, EditPositionVO editPositionVO) {
        // 当岗位名称变更，去修改用户表中的岗位名称
        if (!oldSysPosition.getPostName().equals(editPositionVO.getPostName())) {
            UpdateWrapper<SysUser> updateWrapper = new UpdateWrapper<>();
            updateWrapper.eq("position_id", oldSysPosition.getId())
                    .set("position_name", editPositionVO.getPostName());
            boolean update = sysUserService.update(updateWrapper);
            log.info("岗位名称发送变更后，修改了用户表的{}条岗位名称数据", update);
        }
        oldSysPosition.setBz(editPositionVO.getBz());
        oldSysPosition.setPostCode(editPositionVO.getPostCode());
        oldSysPosition.setPostName(editPositionVO.getPostName());
        oldSysPosition.setPostDesc(editPositionVO.getPostDesc());

        UserInfo userInfo = UserInfoManager.get();

        oldSysPosition.setXtZhxgsj(new Date());
        oldSysPosition.setXtZhxgrid(userInfo.getUserId().toString());
        oldSysPosition.setXtZhxgrxm(userInfo.getUserName());
        oldSysPosition.setXtZhxgrbmid(userInfo.getDeptId().toString());
        oldSysPosition.setXtZhxgrbm(userInfo.getDeptName());

        sysPositionMapper.updateById(oldSysPosition);
    }

}
