package org.jeecg.modules.ctop.service.impl;

import cn.com.ctop.common.module.entity.SysUser;
import cn.com.ctop.common.module.service.ISysUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.jeecg.modules.ctop.entity.UReportFile;
import org.jeecg.modules.ctop.entity.UReportFileBindUser;
import org.jeecg.modules.ctop.mapper.UReportFileMapper;
import org.jeecg.modules.ctop.service.IUReportFileBindUserService;
import org.jeecg.modules.ctop.service.IUReportFileService;
import cn.com.ctop.common.module.utils.ResultMapUtils;
import cn.com.ctop.common.module.utils.StatusCode;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 报表文件存储
 *
 * @author jeecg-boot
 * @version V1.0
 * @date 2020-01-15
 */
@Service
public class UReportFileServiceImpl extends ServiceImpl<UReportFileMapper, UReportFile> implements IUReportFileService {

    @Override
    public boolean checkExistByName(String name) {
        UReportFile file = this.getReportFileByName(name);
        return null != file;
    }

    @Override
    public UReportFile getReportFileByName(String name) {
        QueryWrapper<UReportFile> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("name", name).orderByDesc("id").last("limit 1");
        return this.getOne(queryWrapper);
    }

    @Override
    public List<UReportFile> listAllReportFile() {

        return this.list();
    }

    @Override
    public boolean removeReportFileByName(String name) {
        QueryWrapper<UReportFile> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("name", name);
        return this.remove(queryWrapper);
    }

    @Override
    public boolean saveReportFile(UReportFile entity) {
        entity.setUpdateTime(new Date());
        return this.save(entity);
    }

    @Override
    public boolean updateReportFile(UReportFile entity) {
        entity.setUpdateTime(new Date());
        return this.updateById(entity);
    }

    @Autowired
    private IUReportFileBindUserService bindUserService;

    @Override
    public Map<String, Object> bindUsers(JSONObject data) {
        Map<String, Object> result = new HashMap<>();
        if (null == data) {
            ResultMapUtils.setResultMap(result, StatusCode.COMMON_PARAM_ERROR);
            return result;
        }
        String fileId = data.getString("fileId");
        String userIds = data.getString("userIds");
        if (null == userIds || "".equals(userIds.trim()) || null == fileId || "".equals(fileId.trim())) {
            ResultMapUtils.setResultMap(result, StatusCode.COMMON_PARAM_ERROR);
            return result;
        }
        UReportFile file = getById(fileId);
        if (file == null) {
            result.put("message", "此报表模板不存在或者已经被删除");
            result.put("success", false);
            result.put("code", "-1");
            return result;
        }
        String[] ids = userIds.split(",");
        for (int i = 0; i < ids.length; i++) {
            //用户绑定报表信息
            UReportFileBindUser bindUser = bindUserService.getByUserIdAndFileId(ids[i], fileId);
            if (null != bindUser) {
                continue;
            }
            SysUser sysUser = sysUserService.getById(ids[i]);
            bindUser = new UReportFileBindUser();
            bindUser.setUserId(ids[i]);
            bindUser.setUserName(sysUser.getRealname());
            bindUser.setFileId(fileId);
            bindUser.setFileName(file.getName());
            bindUser.setCreateTime(new Date());
            bindUser.setUpdateTime(new Date());
            bindUserService.save(bindUser);
        }
        result.put("message", "报表模板绑定用户成功");
        result.put("success", true);
        result.put("code", "0");
        return result;
    }

    @Autowired
    private ISysUserService sysUserService;

    @Override
    public Map<String, Object> unbindUser(JSONObject data) {
        Map<String, Object> result = new HashMap<>();
        if (null == data) {
            ResultMapUtils.setResultMap(result, StatusCode.COMMON_PARAM_ERROR);
            return result;
        }
        String userId = data.getString("userId");
        String fileId = data.getString("fileId");
        if (null == userId || "".equals(userId.trim()) || null == fileId || "".equals(fileId.trim())) {
            ResultMapUtils.setResultMap(result, StatusCode.COMMON_PARAM_ERROR);
            return result;
        }
        UReportFile file = getById(fileId);
        if (file == null) {
            result.put("message", "此报表模板不存在或者已经被删除");
            result.put("success", false);
            result.put("code", "-1");
            return result;
        }

        //用户解除绑定
        bindUserService.deleteByUserIdAndFileId(userId, fileId);
        result.put("message", "用户解绑成功");
        result.put("code", "0");
        result.put("success", true);
        return result;
    }
}
