package com.gxwebsoft.oa.service.impl;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gxwebsoft.common.core.web.PageParam;
import com.gxwebsoft.common.core.web.PageResult;
import com.gxwebsoft.common.system.entity.User;
import com.gxwebsoft.common.system.service.UserService;
import com.gxwebsoft.oa.entity.Task;
import com.gxwebsoft.oa.mapper.TaskMapper;
import com.gxwebsoft.oa.param.TaskParam;
import com.gxwebsoft.oa.service.TaskService;
import com.gxwebsoft.oa.service.TaskUserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * 文章记录表Service实现
 *
 * @author WebSoft
 * @since 2022-11-16 11:21:43
 */
@Service
public class TaskServiceImpl extends ServiceImpl<TaskMapper, Task> implements TaskService {
  private Integer userId;
  @Resource
  private UserService userService;
  @Resource
  private TaskUserService taskUserService;

    @Override
    public PageResult<Task> pageRel(TaskParam param) {
        PageParam<Task, TaskParam> page = new PageParam<>(param);
        page.setDefaultOrder("create_time desc");
        List<Task> list = baseMapper.selectPageRel(page, param);
//        list.forEach(d -> {
//          int count = taskUserService.count(new LambdaQueryWrapper<TaskUser>().eq(TaskUser::getTaskId, d.getTaskId()));
//          if(count>0){
//            TaskUserParam taskUserParam = new TaskUserParam();
//            taskUserParam.setTaskId(d.getTaskId());
//            List<TaskUser> users = taskUserService.listRel(taskUserParam);
//            d.setUsers(users);
//          }
//        });
        return new PageResult<>(list, page.getTotal());
    }

  @Override
    public List<Task> listRel(TaskParam param) {
        List<Task> list = baseMapper.selectListRel(param);
        // 排序
        PageParam<Task, TaskParam> page = new PageParam<>();
        //page.setDefaultOrder("create_time desc");
        return page.sortRecords(list);
    }

    @Override
    public Task getByIdRel(Integer taskId) {
        TaskParam param = new TaskParam();
        param.setTaskId(taskId);
      return param.getOne(baseMapper.selectListRel(param));
    }

    @Override
    public ArrayList<Object> getCountNum(User loginUser) {
      Integer type = loginUser.getType();
      Integer userId = loginUser.getUserId();
      AtomicInteger count1 = new AtomicInteger(count(new LambdaQueryWrapper<Task>().eq(Task::getStatus, 0)));
      AtomicInteger count2 = new AtomicInteger(count(new LambdaQueryWrapper<Task>().eq(Task::getStatus, 1)));
      // 非管理员仅可浏览和自己相关的工单
      loginUser.getRoles().forEach(d -> {
        if(!StrUtil.equals(d.getRoleCode(),"superAdmin") && !StrUtil.equals(d.getRoleCode(),"admin")){
          if(type.equals(6)){
            count1.set(count(new LambdaQueryWrapper<Task>().eq(Task::getStatus, 0).eq(Task::getCommander, userId)));
            count2.set(count(new LambdaQueryWrapper<Task>().eq(Task::getStatus, 1).eq(Task::getCommander, userId)));
          }
          if(type.equals(10)){
            count1.set(count(new LambdaQueryWrapper<Task>().eq(Task::getStatus, 0).eq(Task::getPromoter, userId)));
            count2.set(count(new LambdaQueryWrapper<Task>().eq(Task::getStatus, 1).eq(Task::getPromoter, userId)));
          }
        }
      });
      JSONObject json1 = new JSONObject();
      json1.put("tab","待处理");
      json1.put("key","0");
      json1.put("count",count1);
      JSONObject json2 = new JSONObject();
      json2.put("tab","已完成");
      json2.put("key","1");
      json2.put("count",count2);
      ArrayList<Object> objects = new ArrayList<>();
      objects.add(json1);
      objects.add(json2);
      return objects;
    }

  @Override
  public ArrayList<Object> getCountNum(User loginUser, TaskParam param) {
    Integer type = loginUser.getType();
    final Integer userId = param.getUserId();
//    if (param.getUserId() != null) {
//      this.userId = param.getUserId();
//    }else {
//      this.userId = loginUser.getUserId();
//    }

    AtomicInteger count1 = new AtomicInteger(count(new LambdaQueryWrapper<Task>().eq(Task::getStatus, 0)));
    AtomicInteger count2 = new AtomicInteger(count(new LambdaQueryWrapper<Task>().eq(Task::getStatus, 1)));
    // 非管理员仅可浏览和自己相关的工单
    loginUser.getRoles().forEach(d -> {

      if(type.equals(6)){
        count1.set(count(new LambdaQueryWrapper<Task>().eq(Task::getStatus, 0).eq(Task::getCommander, userId)));
        count2.set(count(new LambdaQueryWrapper<Task>().eq(Task::getStatus, 1).eq(Task::getCommander, userId)));
      }
      if(type.equals(10)){
        count1.set(count(new LambdaQueryWrapper<Task>().eq(Task::getStatus, 0).eq(Task::getPromoter, userId)));
        count2.set(count(new LambdaQueryWrapper<Task>().eq(Task::getStatus, 1).eq(Task::getPromoter, userId)));
      }
    });
    JSONObject json1 = new JSONObject();
    json1.put("tab","待处理");
    json1.put("key","0");
    json1.put("count",count1);
    JSONObject json2 = new JSONObject();
    json2.put("tab","已完成");
    json2.put("key","1");
    json2.put("count",count2);
    ArrayList<Object> objects = new ArrayList<>();
    objects.add(json1);
    objects.add(json2);
    return objects;
  }

}
