package com.iotechn.unimall.admin.api.card.impl;

import java.util.List;

import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.iotechn.unimall.core.exception.ServiceException;
import com.iotechn.unimall.data.util.ExcelUtil;
import com.iotechn.unimall.data.mapper.CommonUserInfoMapper;
import com.iotechn.unimall.data.domain.CommonUserInfo;
import com.iotechn.unimall.admin.api.card.ICommonUserInfoService;
import com.iotechn.unimall.data.model.Page;

import java.util.Date;

import org.springframework.transaction.annotation.Transactional;

/**
 * 用户管理Service业务层处理
 *
 * @author jlb
 * @date 2023-04-10
 */
@Service
public class CommonUserInfoServiceImpl implements ICommonUserInfoService {
    @Autowired
    private CommonUserInfoMapper commonUserInfoMapper;

    @Override
    public Boolean add(CommonUserInfo commonUserInfo) throws ServiceException {
        Date now = new Date();
        commonUserInfo.setGmtCreate(now);
        commonUserInfo.setGmtUpdate(now);
        return commonUserInfoMapper.insert(commonUserInfo) > 0;
    }

    @Override
    public Page<CommonUserInfo> list(String nickname, String password, String phone, String realname, Long shareCard, Long lookPage, Long autoAccept, Long status, Date gmtCreate, Date gmtUpdate, Long deleteFlag, Integer page, Integer limit) throws ServiceException {
        Wrapper<CommonUserInfo> wrapper = new EntityWrapper<CommonUserInfo>();
        if (!StringUtils.isEmpty(nickname)) {
            wrapper.eq("nickname", nickname);
        }
        if (!StringUtils.isEmpty(password)) {
            wrapper.eq("password", password);
        }
        if (!StringUtils.isEmpty(phone)) {
            wrapper.eq("phone", phone);
        }
        if (!StringUtils.isEmpty(realname)) {
            wrapper.eq("realname", realname);
        }
        if (!StringUtils.isEmpty(shareCard)) {
            wrapper.eq("share_card", shareCard);
        }
        if (!StringUtils.isEmpty(lookPage)) {
            wrapper.eq("look_page", lookPage);
        }
        if (!StringUtils.isEmpty(autoAccept)) {
            wrapper.eq("auto_accept", autoAccept);
        }
        if (!StringUtils.isEmpty(status)) {
            wrapper.eq("status", status);
        }
        if (!StringUtils.isEmpty(gmtCreate)) {
            wrapper.eq("gmt_create", gmtCreate);
        }
        if (!StringUtils.isEmpty(gmtUpdate)) {
            wrapper.eq("gmt_update", gmtUpdate);
        }
        if (!StringUtils.isEmpty(deleteFlag)) {
            wrapper.eq("delete_flag", deleteFlag);
        }
        wrapper.eq("delete_flag", 0);
        List<CommonUserInfo> list = commonUserInfoMapper.selectPage(new RowBounds((page - 1) * limit, limit), wrapper);
        Integer count = commonUserInfoMapper.selectCount(wrapper);
        return new Page<CommonUserInfo>(list, page, limit, count);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean delete(String id) {
        String[] ids = String.valueOf(id).split(",");
        for (String tt : ids) {
            CommonUserInfo tmp = commonUserInfoMapper.selectById(Long.parseLong(tt));
            if (tmp != null) {
                tmp.setDeleteFlag(1L);
                tmp.setGmtUpdate(new Date());
                commonUserInfoMapper.updateById(tmp);
            }
        }
        return true;
    }

    @Override
    public Boolean update(CommonUserInfo commonUserInfo) throws ServiceException {
        Date now = new Date();
        commonUserInfo.setGmtUpdate(now);
        return commonUserInfoMapper.updateById(commonUserInfo) > 0;
    }

    @Override
    public CommonUserInfo get(Long id) throws ServiceException {
        return commonUserInfoMapper.selectById(id);
    }

    @Override
    public String export(String nickname, String password, String phone, String realname, Long shareCard, Long lookPage, Long autoAccept, Long status, Date gmtCreate, Date gmtUpdate, Long deleteFlag, Integer page, Integer limit) throws ServiceException {
        Wrapper<CommonUserInfo> wrapper = new EntityWrapper<CommonUserInfo>();
        if (!StringUtils.isEmpty(nickname)) {
            wrapper.eq("nickname", nickname);
        }
        if (!StringUtils.isEmpty(password)) {
            wrapper.eq("password", password);
        }
        if (!StringUtils.isEmpty(phone)) {
            wrapper.eq("phone", phone);
        }
        if (!StringUtils.isEmpty(realname)) {
            wrapper.eq("realname", realname);
        }
        if (!StringUtils.isEmpty(shareCard)) {
            wrapper.eq("share_card", shareCard);
        }
        if (!StringUtils.isEmpty(lookPage)) {
            wrapper.eq("look_page", lookPage);
        }
        if (!StringUtils.isEmpty(autoAccept)) {
            wrapper.eq("auto_accept", autoAccept);
        }
        if (!StringUtils.isEmpty(status)) {
            wrapper.eq("status", status);
        }
        if (!StringUtils.isEmpty(gmtCreate)) {
            wrapper.eq("gmt_create", gmtCreate);
        }
        if (!StringUtils.isEmpty(gmtUpdate)) {
            wrapper.eq("gmt_update", gmtUpdate);
        }
        if (!StringUtils.isEmpty(deleteFlag)) {
            wrapper.eq("delete_flag", deleteFlag);
        }
        List<CommonUserInfo> list = commonUserInfoMapper.selectList(wrapper);
        ExcelUtil<CommonUserInfo> util = new ExcelUtil<CommonUserInfo>(CommonUserInfo.class);
        return util.exportExcel(list, "操作日志");
    }
}
