package com.chinaweal.aiccs.aiccs.revoke.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.chinaweal.aiccs.aiccs.revoke.entity.AnnouncementTaskList;
import com.chinaweal.aiccs.aiccs.revoke.entity.dto.AnnouncementTaskListQueryDto;
import com.chinaweal.aiccs.aiccs.revoke.service.AnnouncementTaskListService;
import com.chinaweal.aiccs.common.base.controller.BaseController;
import com.chinaweal.aiccs.common.util.StringUtils;
import com.chinaweal.aicorg.model.AICUser;
import com.chinaweal.aicorg.services.OrgUM;
import com.chinaweal.youfool.framework.springboot.base.query.BaseQuery;
import com.chinaweal.youfool.framework.springboot.rest.RestResult;
import com.chinaweal.youfool.framework.springboot.rest.ResultCode;
import com.chinaweal.youfool.framework.springboot.util.AssertUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;

/**
 * 公告任务名单 Controller
 *
 * @author lroyia
 * @since 2026/2/26 14:12
 **/
@Api(tags = "公告任务名单相关接口")
@RestController
@RequiredArgsConstructor
@RequestMapping("announcement/task")
public class AnnouncementTaskListController extends BaseController {

    private final OrgUM orgUM;
    private final AnnouncementTaskListService announcementTaskListService;

    /**
     * 新增公告任务名单
     *
     * @param announcementTaskList 公告任务名单信息
     * @return 新增结果
     */
    @ApiOperation("新增公告任务名单")
    @PostMapping("/add")
    public RestResult<?> add(@RequestBody AnnouncementTaskList announcementTaskList, HttpServletRequest request) {
        AssertUtils.isNotNull(announcementTaskList);
        AssertUtils.isNotBlank(announcementTaskList.getTaskName(), "任务名称不能为空");

        AICUser loginUser = getLoginUser(request);
        announcementTaskList.setCreateUnitId(loginUser.getOrgID());
        announcementTaskList.setCreateUnitCode(loginUser.getRegionID());
        announcementTaskList.setCreateUnitName(loginUser.getOrgName());
        LocalDateTime now = LocalDateTime.now();
        announcementTaskList.setCreateTime(now);
        announcementTaskList.setUpdateTime(now);
        announcementTaskList.setCreateUserId(loginUser.getPrimaryKey());
        announcementTaskList.setUpdateUserId(loginUser.getPrimaryKey());
        announcementTaskList.setDelFlag("0");

        boolean save = announcementTaskListService.save(announcementTaskList);
        if (save) {
            return RestResult.ok(announcementTaskList);
        }
        return RestResult.error(ResultCode.BUSINESS_LOGIC_ERROR);
    }

    /**
     * 更新公告任务名单
     *
     * @param announcementTaskList 公告任务名单信息
     * @return 更新结果
     */
    @ApiOperation("更新公告任务名单")
    @PostMapping("/update")
    public RestResult<?> update(@RequestBody AnnouncementTaskList announcementTaskList, HttpServletRequest request) {
        AssertUtils.isNotNull(announcementTaskList);
        AssertUtils.isNotBlank(announcementTaskList.getId(), "主键ID不能为空");

        AnnouncementTaskList existEntity = announcementTaskListService.getById(announcementTaskList.getId());
        if (existEntity == null) {
            return RestResult.error(ResultCode.DATA_NONE, "数据不存在");
        }

        AICUser loginUser = getLoginUser(request);
        LocalDateTime now = LocalDateTime.now();
        announcementTaskList.setUpdateTime(now);
        announcementTaskList.setUpdateUserId(loginUser.getPrimaryKey());
        boolean update = announcementTaskListService.updateById(announcementTaskList);
        if (update) {
            return RestResult.ok(announcementTaskList);
        }
        return RestResult.error(ResultCode.BUSINESS_LOGIC_ERROR);
    }

    /**
     * 删除公告任务名单（逻辑删除）
     *
     * @param id 主键ID
     * @return 删除结果
     */
    @ApiOperation("删除公告任务名单")
    @PostMapping("/delete/{id}")
    public RestResult<?> delete(@ApiParam("主键ID") @PathVariable String id, HttpServletRequest request) {
        AssertUtils.isNotBlank(id);

        AnnouncementTaskList existEntity = announcementTaskListService.getById(id);
        if (existEntity == null) {
            return RestResult.error(ResultCode.DATA_NONE, "数据不存在");
        }

        AICUser loginUser = getLoginUser(request);
        AnnouncementTaskList updateEntity = new AnnouncementTaskList();
        updateEntity.setId(id);
        updateEntity.setDelFlag("1");
        updateEntity.setUpdateTime(LocalDateTime.now());
        updateEntity.setUpdateUserId(loginUser.getPrimaryKey());

        boolean update = announcementTaskListService.updateById(updateEntity);
        if (update) {
            return RestResult.ok();
        }
        return RestResult.error(ResultCode.BUSINESS_LOGIC_ERROR);
    }

    /**
     * 根据ID查询公告任务名单
     *
     * @param id 主键ID
     * @return 查询结果
     */
    @ApiOperation("根据ID查询公告任务名单")
    @GetMapping("/get/{id}")
    public RestResult<?> getById(@ApiParam("主键ID") @PathVariable String id) {
        AssertUtils.isNotBlank(id);

        AnnouncementTaskList announcementTaskList = announcementTaskListService.getById(id);
        if (announcementTaskList != null) {
            return RestResult.ok(announcementTaskList);
        }
        return RestResult.error(ResultCode.DATA_NONE);
    }

    /**
     * 分页查询公告任务名单列表
     *
     * @param query 分页查询参数
     * @return 查询结果
     */
    @ApiOperation("分页查询公告任务名单列表")
    @PostMapping("/page")
    public RestResult<?> page(@RequestBody BaseQuery<AnnouncementTaskListQueryDto> query) {
        AssertUtils.isNotNull(query);

        AnnouncementTaskListQueryDto queryDto = query.getEntity(AnnouncementTaskListQueryDto.class);
        if (queryDto == null) {
            queryDto = new AnnouncementTaskListQueryDto();
        }

        LambdaQueryWrapper<AnnouncementTaskList> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(AnnouncementTaskList::getDelFlag, "0");

        if (StringUtils.isNotBlank(queryDto.getTaskName())) {
            wrapper.like(AnnouncementTaskList::getTaskName, queryDto.getTaskName());
        }
        if (StringUtils.isNotBlank(queryDto.getCreateUnitId())) {
            wrapper.eq(AnnouncementTaskList::getCreateUnitId, queryDto.getCreateUnitId());
        }
        if (StringUtils.isNotBlank(queryDto.getCreateUnitCode())) {
            wrapper.eq(AnnouncementTaskList::getCreateUnitCode, queryDto.getCreateUnitCode());
        }
        if (StringUtils.isNotBlank(queryDto.getCreateUnitName())) {
            wrapper.like(AnnouncementTaskList::getCreateUnitName, queryDto.getCreateUnitName());
        }
        if (StringUtils.isNotBlank(queryDto.getCreateUserId())) {
            wrapper.eq(AnnouncementTaskList::getCreateUserId, queryDto.getCreateUserId());
        }
        if (StringUtils.isNotBlank(queryDto.getCreateTimeStart())) {
            wrapper.ge(AnnouncementTaskList::getCreateTime,
                    LocalDateTime.parse(queryDto.getCreateTimeStart() + " 00:00:00",
                            java.time.format.DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
        }
        if (StringUtils.isNotBlank(queryDto.getCreateTimeEnd())) {
            wrapper.le(AnnouncementTaskList::getCreateTime,
                    LocalDateTime.parse(queryDto.getCreateTimeEnd() + " 23:59:59",
                            java.time.format.DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
        }

        wrapper.orderByDesc(AnnouncementTaskList::getCreateTime);

        return RestResult.ok(announcementTaskListService.page(new Page<>(query.getCurrent(), query.getSize()), wrapper));
    }

    /**
     * 查询所有公告任务名单列表（不分页）
     *
     * @return 查询结果
     */
    @ApiOperation("查询所有公告任务名单列表")
    @GetMapping("/list")
    public RestResult<?> list() {
        LambdaQueryWrapper<AnnouncementTaskList> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(AnnouncementTaskList::getDelFlag, "0")
                .orderByDesc(AnnouncementTaskList::getCreateTime);

        return RestResult.ok(announcementTaskListService.list(wrapper));
    }
}
