package cn.palmte.work.model;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import top.jfunc.common.db.QueryHelper;
import top.jfunc.common.db.bean.Page;
import top.jfunc.common.db.utils.Pagination;

import java.util.List;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author Yuanping Zhang
 * @date 2021/11/1
 */
@Repository
public class ProjectUserTimeRepositoryImpl implements ProjectUserTimeRepositoryCustom {

    @Autowired
    private Pagination pagination;

    @Override
    public Page<ProjectUserTime> project(ConcurrentHashMap<String, String> searchInfo, Admin admin, int pageNumber, int pageSize) {
        QueryHelper helper = new QueryHelper("select put.project_name, put.project_id, put.user_id, put.user_cost, sum(put.user_cost) as costSum, sum(put.user_cost * put.user_salary) as salarySum", "from project_user_time put");
        helper.leftJoin("project proj", "put.project_id = proj.id");
        helper.leftJoin("dept", "proj.dept_id = dept.id");
        helper = searchQuery(helper, searchInfo, admin);
        helper.addGroupProperty("put.project_id");
        return pagination.paginate(helper.getSql(), ProjectUserTime.class, pageNumber, pageSize);
    }

    @Override
    public List<ProjectUserTime> staff(ConcurrentHashMap<String, String> searchInfo, Admin admin) {
        QueryHelper helper = new QueryHelper("select put.user_id, put.user_name, put.user_salary", "from project_user_time put");
        helper.leftJoin("project proj", "put.project_id = proj.id");
        helper.leftJoin("dept", "proj.dept_id = dept.id");
        searchQuery(helper, searchInfo, admin);
        helper.addGroupProperty("put.user_id");
        return pagination.find(helper.getSql(), ProjectUserTime.class);
    }

    @Override
    public List<ProjectUserTime> list(ConcurrentHashMap<String, String> searchInfo, Admin admin, int pageNumber, int pageSize) {
        QueryHelper helper = new QueryHelper("select put.project_name, put.project_id, put.user_id, put.user_cost", "from project_user_time put");
        helper.leftJoin("project proj", "put.project_id = proj.id");
        helper.leftJoin("dept", "proj.dept_id = dept.id");
        searchQuery(helper, searchInfo, admin);
        return pagination.find(helper.getSql(), ProjectUserTime.class);
    }

    @Override
    public Page<ProjectUserTime> individual(ConcurrentHashMap<String, String> searchInfo, Integer id, int pageNumber, int pageSize) {
        QueryHelper helper = new QueryHelper("select put.*", "from project_user_time put");
        helper.addCondition("put.user_id = ?", id);
        helper.addCondition(searchInfo.containsKey("time"),"put.time = ?", searchInfo.get("time"));
        helper.addOrderProperty("created_time", false);
        return pagination.paginate(helper.getSql(), ProjectUserTime.class, pageNumber, pageSize);
    }

    @Override
    public List<ProjectUserTime> sum(ConcurrentHashMap<String, String> searchInfo, Admin admin) {
        QueryHelper helper = new QueryHelper("select put.user_salary, put.user_cost", "from project_user_time put");
        helper.leftJoin("project proj", "put.project_id = proj.id");
        helper.leftJoin("dept", "proj.dept_id = dept.id");
        searchQuery(helper, searchInfo, admin);
        return pagination.find(helper.getSql(), ProjectUserTime.class);
    }

    private QueryHelper searchQuery(QueryHelper helper, ConcurrentHashMap<String, String> searchInfo, Admin admin) {
        helper.addCondition(searchInfo.containsKey("time"),"put.time = ?", searchInfo.get("time"));
        helper.addCondition(searchInfo.containsKey("projectId") && !"-1".equals(searchInfo.get("projectId")), "put.project_id = ?", searchInfo.get("projectId"));
        helper.addCondition(searchInfo.containsKey("deptId") && !"-1".equals(searchInfo.get("deptId")), "dept.id = ?", searchInfo.get("deptId"));
        helper.addCondition(searchInfo.containsKey("userName"), "put.user_name like ?", "%'" + searchInfo.get("userName") + "'%");
        if (admin.getRoleLevel() == 4 && admin.getRoleLevel() == 5) {
            helper.addCondition("proj.dept_id = ?", admin.getDeptId());
        } else if (admin.getRoleLevel() == 7) {
            helper.addCondition("proj.creator_id = ?", admin.getId());
        }
        return helper;
    }
}
