package cn.palmte.work.controller.backend;

import cn.palmte.work.bean.SettleBean;
import cn.palmte.work.model.Admin;
import cn.palmte.work.service.ProjectSummaryService;
import cn.palmte.work.utils.DateKit;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 项目汇总统计
 * @author Yuanping Zhang
 * @date 2021/11/10
 */
@Controller
@RequestMapping("/projectSummary")
public class ProjectSummaryController extends BaseController {
    private static final Logger logger = LoggerFactory.getLogger(ProjectSummaryController.class);

    @Autowired
    private ProjectSummaryService projectSummaryService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public String list(@RequestParam(value = KEYWORDS, required = false) String keywords,
                       @RequestParam(value = PAGE_NUMBER, defaultValue = DEFAULT_PAGE_NUMBER) int pageNumber,
                       @RequestParam(value = PAGE_SIZE, defaultValue = DEFAULT_PAGE_SIZE) int pageSize,
                       Map<String, Object> model) {
        //当前登录人的角色类型
        Admin admin = getAdmin();
        model.put("keywords",keywords);
        ConcurrentHashMap<String, String> searchInfo = getSearchInfo(keywords,model);
        String time;
        if (!searchInfo.containsKey("time")) {
            Calendar instance = Calendar.getInstance();
            instance.setTime(new Date());
            instance.set(Calendar.MONTH, instance.get(Calendar.MONTH));
            Date current = instance.getTime();
            time = DateKit.toStr(current, DateKit.DATE_FORMAT_YEAR_MONTH2);
            model.put("time", time);
        } else {
            time = searchInfo.get("time");
        }
        List<SettleBean> list = projectSummaryService.getList(searchInfo, time, admin);
        model.put("pager", list);

        return "admin/project_statistics";
    }
}
