package cn.gov.customs.demo.government.service;

import cn.gov.customs.cacp.enhance.workflow.dao.RuntimeTaskDao;
import cn.gov.customs.cacp.enhance.workflow.pojo.runtime.RuntimeTask;
import cn.gov.customs.demo.government.dao.GovernmentDao;
import cn.gov.customs.demo.government.pojo.GovernmentInfo;
import cn.gov.customs.demo.government.pojo.GovernmentQueryInfo;
import cn.gov.customs.demo.government.pojo.UserTaskQueryInfo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import lombok.RequiredArgsConstructor;
import org.apache.commons.collections.MapUtils;
import org.springframework.stereotype.Service;

/**
 * TODO
 *
 * @author sunxuewen
 * @since 2024/8/15 10:26
 */
@Service
@RequiredArgsConstructor
public class GovernmentServiceImpl implements GovernmentService {

  private final GovernmentDao dao;

  private final RuntimeTaskDao taskMapper;

  @Override
  public GovernmentInfo create() {
    return new GovernmentInfo();
  }

  @Override
  public int insert(GovernmentInfo info) {
    return this.dao.insert(info);
  }

  @Override
  public int updateWithFields(String appCode, GovernmentInfo form, Map<String, Object> fields) {
    if (MapUtils.isNotEmpty(fields)) {
      return this.dao.updateWithFields(appCode, fields);
    }
    return 0;
  }

  @Override
  public int update(GovernmentInfo info) {
    return this.dao.update(info);
  }

//  @Override
//  public int updateWithFields(String appCode, String formId, Map<String, Object> fields) {
//    if (MapUtils.isNotEmpty(fields)) {
//      return this.dao.updateWithFields(formId, fields);
//    }
//    return 0;
//  }

  @Override
  public int delete(String appCode, String formId) {
    return this.dao.delete(formId);
  }

  @Override
  public PageInfo<GovernmentInfo> getPageList(GovernmentQueryInfo query) {
    PageHelper.startPage(query.getPageIndex(), query.getPageSize());
    List<GovernmentInfo> list = this.dao.getList(query);
    return PageInfo.of(list);
  }

  @Override
  public GovernmentInfo get(String appCode, String formId) {
    return this.dao.get(formId);
  }

  @Override
  public PageInfo<RuntimeTask> getUserTaskListByPage(String appCode, String parentId, String userId, UserTaskQueryInfo query) {
    PageHelper.startPage(query.getPageIndex(), query.getPageSize());
    return PageInfo.of(this.taskMapper.getListByUser(appCode, parentId, userId, query.getType()));
  }

  @Override
  public boolean combine(GovernmentInfo info) {
    GovernmentInfo governmentInfoinfo = this.get(info.getAppCode(), info.getFormId());
    if (Objects.nonNull(governmentInfoinfo)) {
      // 各自写合并内容
      return true;
    } else {
      return false;
    }
  }
}
