package com.xunmei.system.controller;

import cn.hutool.core.date.DateUtil;
import com.xunmei.common.core.domain.R;
import com.xunmei.common.core.utils.Ymd;
import com.xunmei.common.core.web.controller.BaseController;
import com.xunmei.common.core.web.domain.AjaxResult;
import com.xunmei.common.core.web.page.TableDataInfo;
import com.xunmei.common.log.annotation.Log;
import com.xunmei.common.log.enums.BusinessType;
import com.xunmei.common.security.annotation.RequiresPermissions;
import com.xunmei.system.api.domain.SysWorkTime;
import com.xunmei.system.domain.SysWorkTimeSet;
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.*;

import java.util.*;

/**
 * 【请填写功能名称】Controller
 *
 * @author xunmei
 * @date 2023-08-14
 */
@Api(tags = {"SysWorkTime"})
@RestController
@RequestMapping("/work/time")
public class SysWorkTimeController extends BaseController {
    @Autowired
    private ISysWorkTimeService sysWorkTimeService;

    /**
     * 查询【请填写功能名称】列表
     */
    @ApiOperation(value = "查询SysWorkTime列表")
    @RequiresPermissions("system:time:list")
    @GetMapping("/pageList")
    public TableDataInfo<SysWorkTime> list(SysWorkTime sysWorkTime) {

        return sysWorkTimeService.selectPage(sysWorkTime);
    }


    /**
     * 获取【请填写功能名称】详细信息
     */
    @ApiOperation(value = "获取SysWorkTime详细信息")
    @RequiresPermissions("system:time:query")
    @GetMapping(value = "/findOneWorkTimeById/{workTimeId}")
    public AjaxResult getInfo(@PathVariable("workTimeId") String workTimeId) {
        return success(sysWorkTimeService.selectSysWorkTimeById(workTimeId));
    }

    /**
     * 修改作息设置
     */
    @ApiOperation(value = "指定日期配置作息")
    @RequiresPermissions("system:set:monthedit")
    @PostMapping("/month/edit")
    public AjaxResult monthedit(@RequestBody WorkTimeMonthEditNewDto sysWorkTimeSet) {
        Date today = DateUtil.beginOfDay(new Date());
        List<Long> orgIds = sysWorkTimeSet.getOrgIdList();
        List<SysWorkTime> exists = sysWorkTimeService.findByYmd(orgIds, today);
        List<SysWorkTime> workTimeLists = new ArrayList<>();
        for (Long orgId : orgIds) {
            List<SysWorkTime> batchnew = sysWorkTimeService.batchnew(orgId, sysWorkTimeSet.getWorkTimeList());
        }

        sysWorkTimeService.handleTodayChanged(orgIds, sysWorkTimeSet.getWorkTimeList(), exists, today);
        //生成任务
        // TODO: 2023/6/19
//        if(CollectionUtil.isNotEmpty(orgIds)){
//            this.workTimeService.handleTodayChanged(orgIds, request.getWorkTimeList(), exists, today);
//            this.workTimeMonthService.toLogNote("批量操作作息月[ %s ][ %s ]", request.getOrgIdList(), today);
//        }
        return AjaxResult.success();
    }

    /**
     * 新增【请填写功能名称】
     */
    @ApiOperation(value = "新增SysWorkTime")
    @RequiresPermissions("system:time:add")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.INSERT)
    @PostMapping()
    public AjaxResult add(@RequestBody SysWorkTime sysWorkTime) {
        return toAjax(sysWorkTimeService.insertSysWorkTime(sysWorkTime));
    }

    /**
     * 修改【请填写功能名称】
     */
    @ApiOperation(value = "修改SysWorkTime")
    @RequiresPermissions("system:time:edit")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.UPDATE)
    @PostMapping("/saveOneWorkTime")
    public AjaxResult edit(@RequestBody SysWorkTime sysWorkTime) {
        return success(sysWorkTimeService.updateSysWorkTime(sysWorkTime));
    }

    /**
     * 删除【请填写功能名称】
     */
    @ApiOperation(value = "删除SysWorkTime")
    @RequiresPermissions("system:time:remove")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids) {
        return toAjax(sysWorkTimeService.deleteSysWorkTimeByIds(ids));
    }

    @ApiOperation(value = "checkData")
    @RequiresPermissions("system:time:checkData")
    @PostMapping(value = "/checkData")
    public AjaxResult checkData(@RequestBody SysWorkTimeSet sysWorkTime) {
        Map<String, Object> map = new HashMap<>();
        if (sysWorkTime.getId() == null) {
            map = sysWorkTimeService.checkData(sysWorkTime);
        }
        //目前只取数据条数，不取数据，看后续有没有需求在页面展示数据
        return AjaxResult.success(map);
    }

    @GetMapping("/findOrgIdsByYmd")
    R<List<Long>> findOrgIdsByYmd(String toYmd) {
        return R.ok(sysWorkTimeService.findOrgIdsByYmd(toYmd));
    }

    @PostMapping("/findWorkTimeByYmd")
    R<SysWorkTime> findWorkTimeByYmd(@RequestBody Ymd ymd) {
        return R.ok(sysWorkTimeService.findWorkTimeByYmd(ymd.getOrgId(), ymd, ymd.isEnable()));

    }

    @PostMapping("/existByYmd")
    R<Boolean> existByYmd(Ymd ymd) {
        return R.ok(sysWorkTimeService.existByYmd(ymd.getOrgId(), ymd, ymd.isEnable()));
    }
}
