package com.xunmei.system.controller;

import cn.hutool.core.date.DateUtil;
import com.xunmei.common.core.web.controller.BaseController;
import com.xunmei.common.core.web.domain.AjaxResult;
import com.xunmei.system.api.domain.SysWorkTime;
import com.xunmei.system.dto.WorkTimeMonthEditNewDto;
import com.xunmei.system.service.ISysWorkTimeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.Date;
import java.util.List;

@Api(tags = "APP端作息月统计")
@RestController
@RequestMapping("/api/work/time/month/app")
public class WorkTimeMonthOnAppController extends BaseController {
    @Autowired
    private ISysWorkTimeService sysWorkTimeService;
    @ApiOperation(value = "按月")
    @PostMapping(value = "/find/month")
    AjaxResult findMonth(@RequestBody @Valid SysWorkTime request) {
        List<SysWorkTime> workTimeOnAppVos = sysWorkTimeService.findByOrgAndMonth(request);
        return success(workTimeOnAppVos);
    }
    @ApiOperation(value = "编辑")
    @PostMapping(value = "/edit")
    AjaxResult edit(@RequestBody @Valid final WorkTimeMonthEditNewDto sysWorkTimeSet) {
        Date today = DateUtil.beginOfDay(new Date());
        List<Long> orgIds = sysWorkTimeSet.getOrgIdList();
        List<SysWorkTime> exists = sysWorkTimeService.findByYmd(sysWorkTimeSet.getOrgIdList(), today);
        for (Long orgId : orgIds) {
            List<SysWorkTime> batchnew = sysWorkTimeService.batchnew(orgId, sysWorkTimeSet.getWorkTimeList());
        }

        sysWorkTimeService.handleTodayChanged(sysWorkTimeSet.getOrgIdList(),sysWorkTimeSet.getWorkTimeList(), exists, today);
//        this.workTimeMonthService.toLogNote("批量操作作息月[ %s ]", request.getOrgIdList());
        return success();
    }
}
