package com.xjrsoft.module.oa.controller;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.xjrsoft.common.constant.GlobalConstant;
import com.xjrsoft.common.model.result.RT;
import com.xjrsoft.module.oa.dto.AddScheduleDto;
import com.xjrsoft.module.oa.dto.UpdateScheduleDto;
import com.xjrsoft.module.oa.entity.Schedule;
import com.xjrsoft.module.oa.service.IScheduleService;
import com.xjrsoft.module.oa.vo.ScheduleListVo;
import com.xjrsoft.module.oa.vo.ScheduleVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 日程管理 前端控制器
 * </p>
 *
 * @author zlf
 * @since 2022-06-16
 */
@RestController
@RequestMapping(GlobalConstant.OA_MODULE_PREFIX + "/schedule")
@Api(value = GlobalConstant.OA_MODULE_PREFIX + "/schedule", tags = "日程管理")
@AllArgsConstructor
public class ScheduleController {

    private final IScheduleService scheduleService;


    @GetMapping
    @ApiOperation(value = "获取日程管理列表")
    public RT<List<ScheduleListVo>> list() {
        List<Schedule> scheduleList = scheduleService.list(Wrappers.<Schedule>query().lambda()
                .eq(Schedule::getEnabledMark, 1)
                .eq(Schedule::getCreateUserId, StpUtil.getLoginIdAsLong())
                .orderByAsc(Schedule::getSortCode));
        List<ScheduleListVo> resultList = new ArrayList<>(scheduleList.size());
        for (Schedule schedule : scheduleList) {
            ScheduleListVo scheduleListVo = new ScheduleListVo();
            scheduleListVo.setId(schedule.getId());
            scheduleListVo.setTitle(schedule.getScheduleContent());
            String endTime = schedule.getEndTime();
            if (!StrUtil.isEmpty(endTime)) {
                String[] endTimeArray = StrUtil.splitToArray(endTime, ":");
                scheduleListVo.setEnd(schedule.getEndDate().withHour(Integer.parseInt(endTimeArray[0])).withMinute(Integer.parseInt(endTimeArray[1])).withSecond(Integer.parseInt(endTimeArray[2])));
            }
            String startTime = schedule.getStartTime();
            if (!StrUtil.isEmpty(startTime)) {
                String[] startTimeArray = StrUtil.splitToArray(endTime, ":");
                scheduleListVo.setStart(schedule.getStartDate().withHour(Integer.parseInt(startTimeArray[0])).withMinute(Integer.parseInt(startTimeArray[1])).withSecond(Integer.parseInt(startTimeArray[2])));
            }
            scheduleListVo.setAllDay(false);
            resultList.add(scheduleListVo);
        }
        return RT.ok(resultList);
    }

    @PostMapping
    @ApiOperation(value = "添加日程管理")
    public RT<Boolean> add(@RequestBody AddScheduleDto scheduleDto) {
        Schedule schedule = BeanUtil.toBean(scheduleDto, Schedule.class);
        //添加消息通知
        boolean save = scheduleService.save(schedule);
        return RT.ok(save);
    }

    @PutMapping
    @ApiOperation(value = "修改日程管理")
    public RT<Boolean> update(@RequestBody UpdateScheduleDto scheduleDto) {
        Schedule schedule = BeanUtil.toBean(scheduleDto, Schedule.class);
        return RT.ok(scheduleService.updateById(schedule));
    }

    @DeleteMapping
    @ApiOperation(value = "删除日程管理")
    public RT<Boolean> delete(@RequestBody List<Long> ids) {
        return RT.ok(scheduleService.delete(ids));
    }

    @GetMapping(value = "/info")
    @ApiOperation(value = "日程详情")
    public RT<ScheduleVo> info(@RequestParam Long id) {
        Schedule schedule = scheduleService.getById(id);
        if (schedule == null) {
            RT.error("找不到当前日程！");
        }
        return RT.ok(BeanUtil.toBean(schedule, ScheduleVo.class));
    }
}
