package com.lz.modules.app.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lz.common.utils.PageUtils;
import com.lz.common.utils.R;
import com.lz.common.utils.StringUtil;
import com.lz.modules.app.dto.DepartmentsDto;
import com.lz.modules.app.entity.DepartmentsStaffRelateEntity;
import com.lz.modules.app.enums.ResultRecordStatusEnum;
import com.lz.modules.app.req.ReportListReq;
import com.lz.modules.app.req.ResultDistributionReq;
import com.lz.modules.app.resp.OwnResultResp;
import com.lz.modules.app.resp.ReportChartResp;
import com.lz.modules.app.service.DepartmentsService;
import com.lz.modules.app.service.DepartmentsStaffRelateService;
import com.lz.modules.app.service.StaffService;
import com.lz.modules.flow.entity.StaffRole;
import com.lz.modules.flow.service.StaffRoleService;
import com.lz.modules.sys.controller.AbstractController;
import com.lz.modules.sys.service.app.ChartService;
import com.lz.modules.sys.service.app.ResultRecordService;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @Author: djc
 * @Desc:
 * @Date: 2020/9/11 10:22
 */
@RestController
@RequestMapping("/result")
public class ReportResultController extends AbstractController{
    @Autowired
    private ResultRecordService resultRecordService;
    @Autowired
    private DepartmentsStaffRelateService departmentsStaffRelateService;
    @Autowired
    private DepartmentsService departmentsService;
    @Autowired
    private StaffService staffService;
    @Autowired
    private ChartService chartService;
    @Autowired
    private StaffRoleService staffRoleService;


    @RequestMapping("chart")
    public R reportChart(ResultDistributionReq req){
        Long userId = getUserId();
        if(!chartService.hrOrBoss(userId)){
            return R.ok();
        }
        if(StringUtil.isBlank(req.getDepartmentId())){
            req.setDepartmentId("1");
        }
        ReportChartResp reportChartResp = chartService.reportChart(req.getSelectMonthTime(), req.getDepartmentId());
        return R.ok().put("data",reportChartResp);
    }




    @RequestMapping("/report")
    public R list(ReportListReq req){
        Long userId = getUserId();
        if(!chartService.hrOrBoss(userId)){
            if(!chartService.leader(userId)){
                return R.ok();
            }
            if(StringUtil.isBlank(req.getDepartmentId())){
                DepartmentsStaffRelateEntity departmentsStaffRelateEntity = departmentsStaffRelateService.selectByStaffId(userId);
                req.setDepartmentId(departmentsStaffRelateEntity.getDepartmentId());
            }
        }
        if(StringUtil.isBlank(req.getDepartmentId())){
            req.setDepartmentId("1");
        }
        PageUtils pageUtils = chartService.resultReportList(req);
        return R.ok().put("page",pageUtils);
    }


    @RequestMapping("/distribution")
    public R distribution(ResultDistributionReq req){
        if(!chartService.hrOrBoss(getUserId())){
            return R.ok();
        }
        if(StringUtil.isBlank(req.getDepartmentId())){
            req.setDepartmentId("1");
        }
        PageUtils pageUtils = chartService.reportDistribution(req);
        return R.ok().put("page",pageUtils);

    }


    @RequestMapping("/departmentTreeByStaffId")
    public R departmentTreeByStaffId(){
        Long userId = getUserId();
        if(chartService.hrOrBoss(userId)){
            List<DepartmentsDto> data = departmentsService.getDepartmentTree(0);
            return  R.ok().put("data",data);
        }
        if(chartService.leader(getUserId())){
            List<DepartmentsDto> data = departmentsService.getDepartmentTreeByStaffId(String.valueOf(userId),true);
            return  R.ok().put("data",data);
        }
        return R.ok();



    }


}







    //获取日期之内的月份
  /*  private static List<String> getMonthBetween(String minDate, String maxDate){
        ArrayList<String> result = new ArrayList<String>();
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");//格式化为年月

            Calendar min = Calendar.getInstance();
            Calendar max = Calendar.getInstance();
            min.setTime(sdf.parse(minDate));
            min.set(min.get(Calendar.YEAR), min.get(Calendar.MONTH), 1);

            max.setTime(sdf.parse(maxDate));
            max.set(max.get(Calendar.YEAR), max.get(Calendar.MONTH), 2);

            Calendar curr = min;
            while (curr.before(max)) {
                result.add(sdf.format(curr.getTime()));
                curr.add(Calendar.MONTH, 1);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return result;
    }
*/
