package com.yunzhupaas.base.controller;

import cn.hutool.core.bean.BeanUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import com.yunzhupaas.base.ActionResult;
import com.yunzhupaas.base.entity.DictionaryDataEntity;
import com.yunzhupaas.base.entity.ScheduleNewEntity;
import com.yunzhupaas.base.entity.ScheduleNewUserEntity;
import com.yunzhupaas.base.model.schedule.*;
import com.yunzhupaas.base.service.DictionaryDataService;
import com.yunzhupaas.base.service.ScheduleNewApi;
import com.yunzhupaas.base.service.ScheduleNewService;
import com.yunzhupaas.base.service.ScheduleNewUserService;
import com.yunzhupaas.base.vo.ListVO;
import com.yunzhupaas.constant.MsgCode;
import com.yunzhupaas.message.entity.SendMessageConfigEntity;
import com.yunzhupaas.message.service.SendMessageConfigService;
import com.yunzhupaas.permission.entity.UserEntity;
import com.yunzhupaas.permission.service.UserService;
import com.yunzhupaas.util.DateUtil;
import com.yunzhupaas.util.JsonUtil;
import com.yunzhupaas.util.RandomUtil;
import com.yunzhupaas.util.StringUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 日程
 *
 * @author 云筑产品开发平台组
 * @copyright 深圳市乐程软件有限公司（http://www.szlecheng.cn）
 */
@Tag(name = "日程", description = "Schedule")
@RestController
@RequestMapping("/api/system/Schedule")
public class ScheduleNewController extends SuperController<ScheduleNewService, ScheduleNewEntity> implements ScheduleNewApi {


    @Autowired
    private UserService userService;
    @Autowired
    private SendMessageConfigService messageTemplateConfigService;
    @Autowired
    private DictionaryDataService dictionaryDataService;
    @Autowired
    private ScheduleNewService scheduleNewService;
    @Autowired
    private ScheduleNewUserService scheduleNewUserService;

    /**
     * 获取日程安排列表
     *
     * @param scheduleNewTime 分页模型
     * @return
     */
    @Operation(summary = "获取日程安排列表")
    @GetMapping
    public ActionResult<ListVO<ScheduleNewListVO>> list(ScheduleNewTime scheduleNewTime) {
        List<ScheduleNewEntity> list = scheduleNewService.getList(scheduleNewTime);
        Date start = DateUtil.stringToDates(scheduleNewTime.getStartTime());
        Date end = DateUtil.stringToDates(scheduleNewTime.getEndTime());
        List<Date> dataAll = DateUtil.getAllDays(start, end);
        List<ScheduleNewEntity> result = new ArrayList<>();
        if (list.size() > 0) {
            for (Date date : dataAll) {
                for (ScheduleNewEntity entity : list) {
                    Date startDay = DateUtil.stringToDates(DateUtil.daFormat(entity.getStartDay()));
                    Date endDay = DateUtil.stringToDates(DateUtil.daFormat(entity.getEndDay()));
                    if(DateUtil.isEffectiveDate(date,startDay,endDay)){
                        result.add(entity);
                    }
                }
            }
        }
        for (ScheduleNewEntity item : result) {
            ScheduleNewEntity entity = BeanUtil.copyProperties(item, ScheduleNewEntity.class);
            if (entity.getAllDay() == 1) {
                entity.setEndDay(DateUtil.dateAddSeconds(entity.getEndDay(), 1));
            }
        }
        List<ScheduleNewListVO> vo = JsonUtil.getJsonToList(result, ScheduleNewListVO.class);
        ListVO listVO = new ListVO();
        listVO.setList(vo);
        return ActionResult.success(listVO);
    }

    /**
     * 获取日程安排列表
     *
     * @param scheduleNewTime 分页模型
     * @return
     */
    @Operation(summary = "获取日程安排列表")
    @GetMapping("/AppList")
    public ActionResult<ScheduleNewAppListVO> selectList(ScheduleNewTime scheduleNewTime) {
        Map<String, Object> signMap = new HashMap<>(16);
        List<ScheduleNewEntity> list = scheduleNewService.getList(scheduleNewTime);
        Date start = DateUtil.stringToDates(scheduleNewTime.getStartTime());
        Date end = DateUtil.stringToDates(scheduleNewTime.getEndTime());
        List<Date> dateList = new ArrayList() {{
            add(start);
            add(end);
        }};
        if(StringUtils.isNotEmpty(scheduleNewTime.getDateTime())){
            dateList.add(DateUtil.strToDate(scheduleNewTime.getDateTime()));
        }
        Date minDate = dateList.stream().min(Date::compareTo).get();
        Date maxDate = dateList.stream().max(Date::compareTo).get();
        List<Date> dataAll = DateUtil.getAllDays(minDate, maxDate);
        ScheduleNewAppListVO vo = new ScheduleNewAppListVO();
        String pattern = "yyyyMMdd";
        String dateTime = StringUtils.isEmpty(scheduleNewTime.getDateTime()) ? DateUtil.dateNow(pattern) : scheduleNewTime.getDateTime().replaceAll("-", "");
        List<ScheduleNewEntity> todayList = new ArrayList<>();
        for (Date date : dataAll) {
            String time = DateUtil.dateToString(date, pattern);
            List<ScheduleNewEntity> result = new ArrayList<>();
            for (ScheduleNewEntity entity : list) {
                Date startDay = DateUtil.stringToDates(DateUtil.daFormat(entity.getStartDay()));
                Date endDay = DateUtil.stringToDates(DateUtil.daFormat(entity.getEndDay()));
                if(DateUtil.isEffectiveDate(date,startDay,endDay)){
                    result.add(entity);
                }
            }
            signMap.put(time, result.size());
            if(time.equals(dateTime)){
                todayList.addAll(result);
            }
        }
        vo.setSignList(signMap);
        vo.setTodayList(JsonUtil.getJsonToList(todayList, ScheduleNewListVO.class));
        return ActionResult.success(vo);
    }

    /**
     * 信息
     *
     * @param id 主键
     * @return
     */
    @Operation(summary = "获取日程安排信息")
    @GetMapping("/{id}")
    @Parameters({
            @Parameter(name = "id", description = "主键", required = true),
    })
    public ActionResult<ScheduleNewInfoVO> info(@PathVariable("id") String id) {
        ScheduleNewEntity entity = scheduleNewService.getInfo(id);
        ScheduleNewInfoVO vo = JsonUtil.getJsonToBean(entity, ScheduleNewInfoVO.class);
        if (vo != null) {
            SendMessageConfigEntity config = StringUtil.isNotEmpty(vo.getSend()) ? messageTemplateConfigService.getInfo(vo.getSend()) : null;
            vo.setSendName(config!=null?config.getFullName():"");
            List<String> toUserIds = scheduleNewUserService.getList(entity.getId(),2).stream().map(ScheduleNewUserEntity::getToUserId).collect(Collectors.toList());
            vo.setToUserIds(toUserIds);
            return ActionResult.success(vo);
        }
        return ActionResult.fail(MsgCode.FA001.get());
    }

    /**
     * 信息
     *
     * @param detailModel 查询模型
     * @return
     */
    @Operation(summary = "获取日程安排信息")
    @GetMapping("/detail")
    public ActionResult<ScheduleNewDetailInfoVO> detail(ScheduleDetailModel detailModel) {
        List<ScheduleNewEntity> groupList = scheduleNewService.getGroupList(detailModel);
        ScheduleNewEntity entity = groupList.size() > 0 ? groupList.get(0) : null;
        boolean isVO = entity != null;
        if (isVO) {
            ScheduleNewDetailInfoVO vo = JsonUtil.getJsonToBean(entity, ScheduleNewDetailInfoVO.class);
            DictionaryDataEntity info = dictionaryDataService.getInfo(entity.getCategory());
            vo.setCategory(info != null ? info.getFullName() : "");
            vo.setUrgent("1".equals(vo.getUrgent()) ? "普通" : "2".equals(vo.getUrgent()) ? "重要" : "紧急");
            UserEntity infoById = userService.getInfo(vo.getCreatorUserId());
            vo.setCreatorUserId(infoById != null ? infoById.getRealName() + "/" + infoById.getAccount() : "");
            List<String> toUserIds = scheduleNewUserService.getList(entity.getId(),2).stream().map(ScheduleNewUserEntity::getToUserId).collect(Collectors.toList());
            List<UserEntity> userName = userService.getUserName(toUserIds);
            StringJoiner joiner = new StringJoiner(",");
            for (UserEntity userEntity : userName) {
                joiner.add(userEntity.getRealName() + "/" + userEntity.getAccount());
            }
            vo.setToUserIds(joiner.toString());
            return ActionResult.success(vo);
        }
        return ActionResult.fail(MsgCode.SYS042.get());
    }

    /**
     * 新建
     *
     * @param scheduleCrForm 日程模型
     * @return
     */
    @Operation(summary = "新建日程安排")
    @PostMapping
    @Parameters({
            @Parameter(name = "scheduleCrForm", description = "日程模型",required = true),
    })
    public ActionResult create(@RequestBody @Valid ScheduleNewCrForm scheduleCrForm) {
        if (scheduleCrForm.paramCheck()) {
            return ActionResult.fail(scheduleCrForm.getErrMsg());
        }
        ScheduleNewEntity entity = JsonUtil.getJsonToBean(scheduleCrForm, ScheduleNewEntity.class);
        try {
            scheduleNewService.create(entity, scheduleCrForm.getToUserIds(), RandomUtil.uuId(),"1",new ArrayList<>());
        } catch (Exception e) {
            e.printStackTrace();
            return ActionResult.fail(e.getMessage());
        }
        return ActionResult.success(MsgCode.SU001.get());
    }

    /**
     * 更新
     *
     * @param id             主键
     * @param scheduleUpForm 日程模型
     * @param type           1.此日程 2.此日程及后续 3.所有日程
     * @return
     */
    @Operation(summary = "更新日程安排")
    @PutMapping("/{id}/{type}")
    @Parameters({
            @Parameter(name = "id", description = "主键", required = true),
            @Parameter(name = "scheduleUpForm", description = "日程模型", required = true),
            @Parameter(name = "type", description = "类型", required = true),
    })
    public ActionResult update(@PathVariable("id") String id, @RequestBody @Valid ScheduleNewUpForm scheduleUpForm, @PathVariable("type") String type) {
        if("1".equals(type)){
            scheduleUpForm.setRepeatTime(null);
            scheduleUpForm.setRepetition(1);
        }
        if (scheduleUpForm.paramCheck()) {
            return ActionResult.fail(scheduleUpForm.getErrMsg());
        }
        ScheduleNewEntity entity = JsonUtil.getJsonToBean(scheduleUpForm, ScheduleNewEntity.class);
        boolean flag = false;
        try {
            flag = scheduleNewService.update(id, entity, scheduleUpForm.getToUserIds(), type);
        } catch (Exception e) {
            e.printStackTrace();
            return ActionResult.fail(e.getMessage());
        }
        if (flag == false) {
            return ActionResult.fail(MsgCode.FA002.get());
        }
        return ActionResult.success(MsgCode.SU004.get());
    }

    /**
     * 删除
     *
     * @param id   主键
     * @param type           1.此日程 2.此日程及后续 3.所有日程
     * @return
     */
    @Operation(summary = "删除日程安排")
    @DeleteMapping("/{id}/{type}")
    @Parameters({
            @Parameter(name = "id", description = "主键", required = true),
            @Parameter(name = "type", description = "类型", required = true),
    })
    public ActionResult delete(@PathVariable("id") String id, @PathVariable("type") String type) {
        ScheduleNewEntity entity = scheduleNewService.getInfo(id);
        if (entity != null) {
            scheduleNewService.delete(entity, type);
            return ActionResult.success(MsgCode.SU003.get());
        }
        return ActionResult.fail(MsgCode.FA003.get());
    }

}

