package com.bofeng.service;

import com.baomidou.mybatisplus.toolkit.IdWorker;
import com.bofeng.dao.QzTaskDao;
import com.bofeng.dao.YeweihuiSysOwnerMapper;
import com.bofeng.dao.YeweihuiUserRoleMapper;
import com.bofeng.entity.QzTask;
import lombok.var;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional(readOnly = true)
public class QzTaskService {

  @Autowired
  private QzTaskDao qzTaskDao;

  @Autowired
  private YeweihuiUserRoleMapper yeweihuiUserRoleMapper;

  @Autowired
  private YeweihuiSysOwnerMapper yeweihuiSysOwnerMapper;

  public List<QzTask> selectAll(Long userId) {

    Long propertyId = yeweihuiUserRoleMapper.queryPropertyIdByUserId(userId);

    Long uptownId = yeweihuiSysOwnerMapper.queryUptownIdByUserId(propertyId);

    return qzTaskDao.selectAll(uptownId);
  }

  public List<QzTask> queryByHouseNumber(String houseNumber, Integer status) {
    return qzTaskDao.queryByHouseNumber(houseNumber, status);
  }

  public QzTask queryByTaskId(Long taskId) {
    return qzTaskDao.queryByTaskId(taskId);
  }

  //求助的新增
  @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
  public int addHelp(String taskTitle, String taskTarget, String remark, Long userId) {
    var qzTak = new QzTask();
    qzTak.setTaskId(IdWorker.getId());
    qzTak.setTaskTitle(taskTitle);
    qzTak.setRemark(remark);
    qzTak.setTaskTarget(taskTarget);
    qzTak.setTaskStatus(1);
    qzTak.setUserId(userId);
    //当前用户名称
    qzTak.setUserName(qzTaskDao.selectUserName(userId).getUserName());
    //当前用户的门牌id  门牌号
    QzTask qzTask1 = qzTaskDao.selectHome(userId);
    qzTak.setHouseId(qzTask1.getHouseId());
    //获取当前小区id
    qzTak.setUptownId(qzTask1.getUptownId());
    qzTak.setHouseNumber(qzTask1.getHouseNumber());
    qzTak.setUserCreate(userId);
    qzTak.setTimeCreate(new DateTime());
    qzTak.setUserUpdate(userId);
    qzTak.setTimeUpdate(new DateTime());
    return qzTaskDao.insert(qzTak);
  }

  public List<QzTask> queryQzTaskByStatus(Integer taskStatus) {
    return qzTaskDao.queryQzTaskByStatus(taskStatus);
  }

  public List<QzTask> queryQzTask(Integer taskStatus, String taskTitle, Long userId) {
    return qzTaskDao.queryQzTask(taskStatus, taskTitle, userId);
  }

  @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
  public int taskQueryTaskId(Long taskId) {
    return qzTaskDao.updateStatusById(taskId, 3);//求助完成
  }
}
