package com.yanzhu.manage.controller;

import com.yanzhu.common.core.annotation.Excel;
import com.yanzhu.common.core.utils.DateUtils;
import com.yanzhu.common.core.utils.poi.ExcelUtil;
import com.yanzhu.common.core.web.controller.BaseController;
import com.yanzhu.common.core.web.domain.AjaxResult;
import com.yanzhu.common.core.web.page.TableDataInfo;
import com.yanzhu.common.log.annotation.Log;
import com.yanzhu.common.log.enums.BusinessType;
import com.yanzhu.common.security.annotation.RequiresPermissions;
import com.yanzhu.manage.domain.AttendanceUbiData;
import com.yanzhu.manage.service.IAttendanceUbiDataService;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.util.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.*;

/**
 * 考勤管理Controller
 * 
 * @author yanzhu
 * @date 2024-10-12
 */
@RestController
@RequestMapping("/attendanceUbiData")
public class AttendanceUbiDataController extends BaseController
{
    @Autowired
    private IAttendanceUbiDataService attendanceUbiDataService;


    /**
     * 查询考勤管理列表
     */
    @RequiresPermissions("manage:attendanceUbiData:list")
    @GetMapping("/list")
    public TableDataInfo list(AttendanceUbiData attendanceUbiData)
    {
        startPage();
        List<AttendanceUbiData> list = attendanceUbiDataService.selectAttendanceUbiDataList(attendanceUbiData);
        return getDataTable(list);
    }

    @RequiresPermissions("manage:attendanceUbiData:list")
    @GetMapping("/statistics")
    public TableDataInfo statistics(AttendanceUbiData attendanceUbiData){
        startPage();
        List<AttendanceUbiData> list = attendanceUbiDataService.queryStatistics(attendanceUbiData);
        TableDataInfo data=getDataTable(list);
        List<AttendanceUbiData> datas= (List<AttendanceUbiData>) data.getRows();
        if(datas.size()>0){
            List<Long> userIds=new ArrayList<>();
            for(AttendanceUbiData ad:datas){
                userIds.add(ad.getUserId());
            }
            attendanceUbiData.setUserIds(userIds);
            List<AttendanceUbiData> userDatas=attendanceUbiDataService.queryStatisticsByUserIds(attendanceUbiData);
            //处理汇总数据
            List<AttendanceUbiData> retUsers= attendanceUbiDataService.procStatistics(attendanceUbiData.getInTime(), datas,userDatas);
            data.setRows(retUsers);
        }
        return  data;
    }

    @RequiresPermissions("manage:attendanceUbiData:list")
    @PostMapping("/statisticsExport")
    public void statisticsExport(HttpServletResponse response,AttendanceUbiData attendanceUbiData){
        List<AttendanceUbiData> list = attendanceUbiDataService.queryStatistics(attendanceUbiData);
        if(list.size()>0){
            List<Long> userIds=new ArrayList<>();
            for(AttendanceUbiData ad:list){
                userIds.add(ad.getUserId());
            }
            attendanceUbiData.setUserIds(userIds);

            List<AttendanceUbiData> userDatas=attendanceUbiDataService.queryStatisticsByUserIds(attendanceUbiData);
            Date inTime=attendanceUbiData.getInTime();
            Calendar calendar = new GregorianCalendar(inTime.getYear(), inTime.getMonth(), 1);
            int daysInMonth = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
            //处理汇总数据
            List<AttendanceUbiData> retUsers= attendanceUbiDataService.procStatistics(attendanceUbiData.getInTime(), list,userDatas);
            ExcelUtil<AttendanceUbiData> util = new ExcelUtil<AttendanceUbiData>(AttendanceUbiData.class){
                @Override
                public void writeSheet() {
                    createSheet(0,0);
                    Row row = sheet.createRow(0);
                    Cell cell=row.createCell(0);
                    cell.setCellValue("公司");
                    cell=row.createCell(1);
                    cell.setCellValue("项目");
                    cell=row.createCell(2);
                    cell.setCellValue("用户");
                    cell=row.createCell(3);
                    cell.setCellValue("班组");
                    cell=row.createCell(4);
                    cell.setCellValue("工种类型");
                    cell=row.createCell(5);
                    cell.setCellValue("工种岗位");
                    for(int i=1;i<=daysInMonth;i++){
                        cell=row.createCell(i+5);
                        cell.setCellValue(i);
                    }
                    for(int i=0;i<this.list.size();i++){
                        AttendanceUbiData item=list.get(i);
                        row=sheet.createRow(i+1);
                        cell=row.createCell(0);
                        cell.setCellValue(item.getComName());
                        cell=row.createCell(1);
                        cell.setCellValue(item.getProjectName());
                        cell=row.createCell(2);
                        cell.setCellValue(item.getUserName());
                        cell=row.createCell(3);
                        cell.setCellValue(item.getSubDeptGroupName());
                        cell=row.createCell(4);
                        cell.setCellValue(item.getCraftType());
                        cell=row.createCell(5);
                        cell.setCellValue(item.getCraftPost());
                        for(int j=1;j<=daysInMonth;j++){
                            cell=row.createCell(5+j);
                            cell.setCellValue(item.getDays().get(j-1));
                        }
                    }
                }
            };

            response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
            response.setCharacterEncoding("utf-8");
            util.init(list, "考勤管理数据", "", Excel.Type.EXPORT);
            util.exportExcel(response);
        }
    }

    /**
     * 导出考勤管理列表
     */
    @RequiresPermissions("manage:attendanceUbiData:export")
    @Log(title = "考勤管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, AttendanceUbiData attendanceUbiData)
    {
        List<AttendanceUbiData> list = attendanceUbiDataService.selectAttendanceUbiDataList(attendanceUbiData);
        ExcelUtil<AttendanceUbiData> util = new ExcelUtil<AttendanceUbiData>(AttendanceUbiData.class);
        util.exportExcel(response, list, "考勤管理数据");
    }

    /**
     * 获取考勤管理详细信息
     */
    @RequiresPermissions("manage:attendanceUbiData:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(attendanceUbiDataService.selectAttendanceUbiDataById(id));
    }

    /**
     * 新增考勤管理
     */
    @RequiresPermissions("manage:attendanceUbiData:add")
    @Log(title = "考勤管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody AttendanceUbiData attendanceUbiData)
    {
        return toAjax(attendanceUbiDataService.insertAttendanceUbiData(attendanceUbiData));
    }

    /**
     * 修改考勤管理
     */
    @RequiresPermissions("manage:attendanceUbiData:edit")
    @Log(title = "考勤管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody AttendanceUbiData attendanceUbiData)
    {
        return toAjax(attendanceUbiDataService.updateAttendanceUbiData(attendanceUbiData));
    }

    /**
     * 删除考勤管理
     */
    @RequiresPermissions("manage:attendanceUbiData:remove")
    @Log(title = "考勤管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(attendanceUbiDataService.deleteAttendanceUbiDataByIds(ids));
    }
}
