WIP: Resolve "Статистика по активностям"
#232
Draft
arefiev1997 wants to merge 11 commits from 93-activites-analytics
into dev
@ -0,0 +1,17 @@
|
||||
package ru.ulstu.activity.controller;
|
||||
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import ru.ulstu.activity.service.ActivityService;
|
||||
|
||||
@Controller()
|
||||
@RequestMapping(value = "/activities")
|
||||
public class ActivityController {
|
||||
private ActivityService activityService;
|
||||
|
||||
public ActivityController(ActivityService activityService) {
|
||||
this.activityService = activityService;
|
||||
}
|
||||
|
||||
|
||||
}
|
@ -0,0 +1,81 @@
|
||||
package ru.ulstu.activity.model;
|
||||
|
||||
import org.springframework.format.annotation.DateTimeFormat;
|
||||
import ru.ulstu.core.model.BaseEntity;
|
||||
import ru.ulstu.user.model.User;
|
||||
|
||||
import javax.persistence.Column;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.EnumType;
|
||||
import javax.persistence.Enumerated;
|
||||
import javax.persistence.JoinColumn;
|
||||
import javax.persistence.ManyToOne;
|
||||
import javax.persistence.Table;
|
||||
import javax.persistence.Temporal;
|
||||
import javax.persistence.TemporalType;
|
||||
import java.util.Date;
|
||||
|
||||
@Entity
|
||||
@Table(name = "activity")
|
||||
public class Activity extends BaseEntity {
|
||||
|
||||
public enum ActivityState {
|
||||
TOOK_IN_WORK("Взято в работу"),
|
||||
COMPLETED("Выполнено");
|
||||
|
||||
private String statusName;
|
||||
|
||||
ActivityState(String statusName) {
|
||||
this.statusName = statusName;
|
||||
}
|
||||
|
||||
public String getStatusName() {
|
||||
return this.statusName;
|
||||
}
|
||||
}
|
||||
|
||||
@ManyToOne()
|
||||
@JoinColumn(name = "user_id")
|
||||
private User user;
|
||||
|
||||
@Temporal(value = TemporalType.TIMESTAMP)
|
||||
@DateTimeFormat(pattern = "yyyy-MM-dd")
|
||||
private Date date;
|
||||
|
||||
@Enumerated(value = EnumType.STRING)
|
||||
@Column(name = "state")
|
||||
private ActivityState activityState;
|
||||
|
||||
public Activity(User user, ActivityState activityState) {
|
||||
this.user = user;
|
||||
this.date = new Date();
|
||||
this.activityState = activityState;
|
||||
}
|
||||
|
||||
public Activity() {
|
||||
}
|
||||
|
||||
public User getUser() {
|
||||
return user;
|
||||
}
|
||||
|
||||
public void setUser(User user) {
|
||||
this.user = user;
|
||||
}
|
||||
|
||||
public Date getDate() {
|
||||
return date;
|
||||
}
|
||||
|
||||
public void setDate(Date date) {
|
||||
this.date = date;
|
||||
}
|
||||
|
||||
public ActivityState getActivityState() {
|
||||
return activityState;
|
||||
}
|
||||
|
||||
public void setActivityState(ActivityState activityState) {
|
||||
this.activityState = activityState;
|
||||
}
|
||||
}
|
@ -0,0 +1,38 @@
|
||||
package ru.ulstu.activity.model;
|
||||
|
||||
public class ActivityElement {
|
||||
private int startedCount = 0;
|
||||
private int completedCount = 0;
|
||||
|
||||
public ActivityElement() {
|
||||
}
|
||||
|
||||
public ActivityElement(int startedCount, int completedCount) {
|
||||
this.startedCount = startedCount;
|
||||
this.completedCount = completedCount;
|
||||
}
|
||||
|
||||
public int getStartedCount() {
|
||||
return startedCount;
|
||||
}
|
||||
|
||||
public void setStartedCount(int startedCount) {
|
||||
this.startedCount = startedCount;
|
||||
}
|
||||
|
||||
public int getCompletedCount() {
|
||||
return completedCount;
|
||||
}
|
||||
|
||||
public void setCompletedCount(int completedCount) {
|
||||
this.completedCount = completedCount;
|
||||
}
|
||||
|
||||
public void incrementStartedCount() {
|
||||
this.startedCount++;
|
||||
}
|
||||
|
||||
public void incrementCompletedCount() {
|
||||
this.completedCount++;
|
||||
}
|
||||
}
|
@ -0,0 +1,19 @@
|
||||
package ru.ulstu.activity.repository;
|
||||
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.data.jpa.repository.Query;
|
||||
import org.springframework.data.repository.query.Param;
|
||||
import ru.ulstu.activity.model.Activity;
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
|
||||
public interface ActivityRepository extends JpaRepository<Activity, Integer> {
|
||||
|
||||
@Query("SELECT a FROM Activity a WHERE (:userId IS NULL OR a.user.id = :userId) " +
|
||||
"AND (CAST(:dateFrom AS date) IS NULL OR :dateFrom < a.date) " +
|
||||
"AND (CAST(:dateTo AS date) IS NULL OR :dateTo > a.date)")
|
||||
List<Activity> getByUserAndDateInterval(@Param("userId") Integer userId,
|
||||
@Param("dateFrom") Date dateFrom,
|
||||
@Param("dateTo") Date dateTo);
|
||||
}
|
@ -0,0 +1,56 @@
|
||||
package ru.ulstu.activity.service;
|
||||
|
||||
import org.springframework.stereotype.Service;
|
||||
import ru.ulstu.activity.model.Activity;
|
||||
import ru.ulstu.activity.model.ActivityElement;
|
||||
import ru.ulstu.activity.repository.ActivityRepository;
|
||||
import ru.ulstu.user.model.User;
|
||||
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Date;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
@Service
|
||||
public class ActivityService {
|
||||
private ActivityRepository activityRepository;
|
||||
|
||||
public ActivityService(ActivityRepository activityRepository) {
|
||||
this.activityRepository = activityRepository;
|
||||
}
|
||||
|
||||
public void createActivity(List<User> users, Activity.ActivityState state) {
|
||||
List<Activity> activities = new ArrayList<>();
|
||||
for (User user : users) {
|
||||
activities.add(new Activity(user, state));
|
||||
}
|
||||
activityRepository.save(activities);
|
||||
}
|
||||
|
||||
public Map<String, ActivityElement> getUsersActivity(Integer userId, Date dateFrom, Date dateTo) {
|
||||
List<Activity> activities = activityRepository.getByUserAndDateInterval(userId, dateFrom, dateTo);
|
||||
Map<String, ActivityElement> dateActivities = new HashMap<>();
|
||||
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
|
||||
for (Activity activity : activities) {
|
||||
String dt = formatter.format(activity.getDate());
|
||||
ActivityElement element;
|
||||
if (!dateActivities.containsKey(dt)) {
|
||||
element = new ActivityElement();
|
||||
} else {
|
||||
element = dateActivities.get(dt);
|
||||
}
|
||||
switch (activity.getActivityState()) {
|
||||
case TOOK_IN_WORK:
|
||||
element.incrementStartedCount();
|
||||
break;
|
||||
case COMPLETED:
|
||||
element.incrementCompletedCount();
|
||||
break;
|
||||
}
|
||||
dateActivities.put(dt, element);
|
||||
}
|
||||
return dateActivities;
|
||||
}
|
||||
}
|
@ -0,0 +1,17 @@
|
||||
<?xml version="1.1" encoding="UTF-8" standalone="no"?>
|
||||
<databaseChangeLog xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.1.xsd">
|
||||
<changeSet author="arefyev" id="20190603_000000-3">
|
||||
<createTable tableName="activity">
|
||||
<column name="id" type="integer"/>
|
||||
<column name="user_id" type="integer"/>
|
||||
<column name="version" type="integer"/>
|
||||
<column name="date" type="timestamp"/>
|
||||
<column name="state" type="varchar(255)"/>
|
||||
</createTable>
|
||||
<addForeignKeyConstraint baseTableName="activity" baseColumnNames="user_id"
|
||||
constraintName="fk_activity_user" referencedTableName="users"
|
||||
referencedColumnNames="id"/>
|
||||
</changeSet>
|
||||
</databaseChangeLog>
|
Loading…
Reference in New Issue