package com.xunmei.system.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xunmei.common.core.utils.DateUtils;
import com.xunmei.common.core.web.page.TableDataInfo;
import com.xunmei.common.security.utils.SecurityUtils;
import com.xunmei.system.api.domain.SysUserInformation;
import com.xunmei.system.api.model.LoginUser;
import com.xunmei.system.mapper.SysUserInformationMapper;
import com.xunmei.system.service.ISysOrgService;
import com.xunmei.system.service.ISysUserInformationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

/**
 * 【请填写功能名称】Service业务层处理
 *
 * @author xunmei
 * @date 2023-08-18
 */
@Service
public class SysUserInformationServiceImpl extends ServiceImpl<SysUserInformationMapper, SysUserInformation> implements ISysUserInformationService {
    @Autowired
    private SysUserInformationMapper sysUserInformationMapper;
    @Autowired
    private ISysOrgService orgService;

    @Override
    public TableDataInfo<SysUserInformation> selectPage(SysUserInformation sysUserInformation) {


        //未删除
        sysUserInformation.setDelFlag("0");
        Page<SysUserInformation> page;
        //分页
        if (sysUserInformation.getPageNum() != null && sysUserInformation.getPageSize() != null) {
            page = new Page<>(sysUserInformation.getPageNum(), sysUserInformation.getPageSize());
        } else {
            page = new Page<>();
        }
        //查询条件
        QueryWrapper<SysUserInformation> query = new QueryWrapper<>(sysUserInformation);
        //下穿
/*        if (sysUserInformation.getCheckSub()){
            List<Long> ids = orgService.selectCheckSubOrgIdList(sysUserInformation.getOrgId());
            //清空前端传递的org_id
            sysUserInformation.setOrgId(null);
            //添加in条件
            query.in("org_id",ids);
        }*/
        //时间范围查询
        if (sysUserInformation.getParams().get("beginTime") != null && sysUserInformation.getParams().get("endTime") != null) {
            query.between("create_time", sysUserInformation.getParams().get("beginTime"), sysUserInformation.getParams().get("endTime"));
        }
        //获取数据
        page = sysUserInformationMapper.selectPage(page, query);
        //抓换为TableDataInfo适配前端
        return TableDataInfo.build(page);


    }


    /**
     * 查询【请填写功能名称】
     *
     * @param id 【请填写功能名称】主键
     * @return 【请填写功能名称】
     */
    @Override
    public SysUserInformation selectSysUserInformationById(Long id) {
        return sysUserInformationMapper.selectById(id);
    }

    @Override
    public SysUserInformation getInformationByUserId(Long id) {
        return sysUserInformationMapper.getInformationByUserId(id);
    }

    @Override
    public int extendUser(SysUserInformation sysUserInformation) {
        SysUserInformation informationByUserId = sysUserInformationMapper.getInformationByUserId(sysUserInformation.getUserId());
        int i = 0;
        LoginUser loginUser = SecurityUtils.getLoginUser();
        if (null != informationByUserId) {
            sysUserInformation.setUpdateTime(DateUtils.getNowDate());
            sysUserInformation.setUpdateBy(loginUser.getUsername());
            i = sysUserInformationMapper.updateSysUserInformation(sysUserInformation);
        } else {
            sysUserInformation.setCreateTime(DateUtils.getNowDate());
            sysUserInformation.setUpdateBy(loginUser.getUsername());
            i = sysUserInformationMapper.insert(sysUserInformation);
        }

        return i;
    }

    /**
     * 查询【请填写功能名称】列表
     *
     * @param sysUserInformation 【请填写功能名称】
     * @return 【请填写功能名称】
     */
    @Override
    public List<SysUserInformation> selectSysUserInformationList(SysUserInformation sysUserInformation) {
        return sysUserInformationMapper.selectList(new QueryWrapper<>(sysUserInformation));
    }

    /**
     * 新增【请填写功能名称】
     *
     * @param sysUserInformation 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int insertSysUserInformation(SysUserInformation sysUserInformation) {
        sysUserInformation.setCreateTime(DateUtils.getNowDate());
        return sysUserInformationMapper.insert(sysUserInformation);
    }

    /**
     * 修改【请填写功能名称】
     *
     * @param sysUserInformation 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int updateSysUserInformation(SysUserInformation sysUserInformation) {
        sysUserInformation.setUpdateTime(DateUtils.getNowDate());
        return sysUserInformationMapper.updateById(sysUserInformation);
    }

    /**
     * 批量删除【请填写功能名称】
     *
     * @param ids 需要删除的【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteSysUserInformationByIds(Long[] ids) {
        return sysUserInformationMapper.deleteBatchIds(Arrays.asList((ids)));
    }

    /**
     * 删除【请填写功能名称】信息
     *
     * @param id 【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteSysUserInformationById(Long id) {
        return sysUserInformationMapper.deleteById(id);
    }
}
