package com.yanzhu.jh.bigscreen.web.controller;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.yanzhu.jh.project.domain.SurProjectAttendanceData;
import com.yanzhu.jh.project.domain.SurProjectAttendanceUser;
import com.yanzhu.jh.project.domain.SurProjectDeptWroks;
import com.yanzhu.jh.project.domain.SurProjectWorkAttendance;
import com.yanzhu.jh.project.service.ISurProjectAttendanceDataService;
import com.yanzhu.jh.project.service.ISurProjectAttendanceUserService;
import com.yanzhu.jh.project.service.ISurProjectDeptWroksService;
import com.yanzhu.jh.project.service.ISurProjectWorkAttendanceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * 在岗出勤Conller
 */
@RestController
@RequestMapping("/bgscreen/attendance")
public class ProjectAttendanceController extends BaseController {

    @Autowired
    ISurProjectWorkAttendanceService surProjectWorkAttendanceService;

    @Autowired
    ISurProjectDeptWroksService surProjectDeptWroksService;

    @Autowired
    ISurProjectAttendanceDataService attendanceDataService;

    @Autowired
    ISurProjectAttendanceUserService attendanceUserService;

    @Autowired
    HttpServletRequest request;
    /**
     * 查询在岗人数
     * @param deptId
     * @param projectId
     * @return
     */
    @GetMapping("/getDeptWorksList")
    public TableDataInfo getDeptWorksList(String deptId,String projectId){
        SurProjectDeptWroks surProjectDeptWroks = new SurProjectDeptWroks();

        if(deptId!=null && !"0".equals(deptId)){
            surProjectDeptWroks.setDeptId(Convert.toLong(deptId));
        }else{
            if(SecurityUtils.isUserB()){
                surProjectDeptWroks.setPrjIds(getProjectIds());
            }
        }
        if(projectId!=null && !"0".equals(projectId)){
            surProjectDeptWroks.setProjectId(Convert.toLong(projectId));
        }
        return getDataTable(surProjectDeptWroksService.selectBgscreenDeptWroksList(surProjectDeptWroks));
    }

    @PostMapping("selectList")
    public TableDataInfo selectList(@RequestBody SurProjectDeptWroks where){
        startPage();
        Long deptId= where.getDeptId();
        if(deptId==null || deptId<=0){
            if (SecurityUtils.isUserB()) {
                where.setPrjIds(getProjectIds());
            }
        }
        List<SurProjectDeptWroks> list=surProjectDeptWroksService.selectList(where);
        return getDataTable(list);
    }

    /**
     * 查询出勤列表
     * @param deptId
     * @param projectId
     * @param date
     * @return
     */
    @GetMapping("/getWorkAttendanceList")
    public TableDataInfo getWorkAttendanceList(String deptId,String projectId,String date){
        SurProjectWorkAttendance surProjectWorkAttendance = new SurProjectWorkAttendance();
        if(deptId!=null && !"0".equals(deptId)){
            surProjectWorkAttendance.setDeptId(Convert.toLong(deptId));
        }else{
            if(SecurityUtils.isUserB()){
                surProjectWorkAttendance.setPrjIds(getProjectIds());
            }
        }
        if(projectId!=null && !"0".equals(projectId)){
            surProjectWorkAttendance.setProjectId(Convert.toLong(projectId));
        }
        if(StringUtils.isNotBlank(date)){
            surProjectWorkAttendance.setWorkDate(DateUtils.parseDate(date));
        }else{
            surProjectWorkAttendance.setWorkDate(new Date());
        }
        return getDataTable(surProjectWorkAttendanceService.selectBgscreenWorkAttendanceList(surProjectWorkAttendance));
    }

    /**
     * 今日出勤统计
     * @param where
     * @return
     */
    @PostMapping("/groupByComany")
    public AjaxResult groupByComany(@RequestBody SurProjectAttendanceData where){
        Long deptId= where.getDeptId();
        if(deptId==null || deptId<=0){
            if (SecurityUtils.isUserB()) {
                where.setPrjIds(getProjectIds());
            }
        }
        List<SurProjectAttendanceData> list=attendanceDataService.groupByComany(where);
        return AjaxResult.success(list);
    }

    /**
     * 今日出勤详情
     */
    @PostMapping("/todayAttendance")
    public TableDataInfo todayAttendance(@RequestBody SurProjectAttendanceUser where){
        if(where.getDeptId()==null || where.getDeptId()<=0) {
            if (SecurityUtils.isUserB()) {
                where.setPrjIds(getProjectIds());
            }
        }
        long cnt=attendanceUserService.countTodayAttendance(where);
        List<SurProjectAttendanceUser> list=attendanceUserService.todayAttendance(where);
        TableDataInfo dataInfo=new TableDataInfo();
        dataInfo.setTotal(cnt);
        dataInfo.setRows(list);
        return dataInfo;
    }

}
