package cn.iocoder.yudao.module.system.controller.admin.biz;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.framework.tenant.core.aop.TenantIgnore;
import cn.iocoder.yudao.framework.tenant.core.context.TenantContextHolder;
import cn.iocoder.yudao.module.system.controller.admin.biz.vo.MonthlyActivityPageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.biz.vo.MonthlyActivityRespVO;
import cn.iocoder.yudao.module.system.controller.admin.biz.vo.MonthlyActivitySaveReqVO;
import cn.iocoder.yudao.module.system.controller.admin.biz.vo.excel.MonthlyActivityExcelVO;
import cn.iocoder.yudao.module.system.dal.dataobject.biz.ActivityExecutionRecordDO;
import cn.iocoder.yudao.module.system.dal.dataobject.biz.MonthlyActivityDO;
import cn.iocoder.yudao.module.system.service.biz.MonthlyActivityService;
import cn.iocoder.yudao.module.system.service.tenant.TenantService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import org.springframework.validation.annotation.Validated;
import io.swagger.v3.oas.annotations.tags.Tag;

import java.io.IOException;
import java.time.LocalDate;
import java.util.List;
import java.util.Objects;

import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;


@Tag(name = "管理后台 - 月度活动")
@RestController
@RequestMapping("/elderly/monthlyActivity")
@Validated
public class MonthlyActivityController {

    @Resource
    private MonthlyActivityService monthlyActivityService;
    @Resource
    private TenantService tenantService;


    @PostMapping("/createMonthlyActivity")
    @Operation(summary = "创建月度活动")
    @TenantIgnore
    public CommonResult<Long> createMonthlyActivity(@Valid @RequestBody MonthlyActivitySaveReqVO createReqVO) {
        createReqVO.setTenantId(createReqVO.getTenantId() == null ? TenantContextHolder.getTenantId() : createReqVO.getTenantId());
        return success(monthlyActivityService.createMonthlyActivity(createReqVO));
    }

    @PutMapping("/updateMonthlyActivity")
    @Operation(summary = "更新月度活动")
    @TenantIgnore
    public CommonResult<Boolean> updateMonthlyActivity(@Valid @RequestBody MonthlyActivitySaveReqVO updateReqVO) {
        monthlyActivityService.updateMonthlyActivity(updateReqVO);
        return success(true);
    }


    @PostMapping("/deleteMonthlyActivity")
    @Operation(summary = "删除月度活动")
    @TenantIgnore
    public CommonResult<Boolean> deleteMonthlyActivity(@RequestBody Long[] ids) {
        monthlyActivityService.deleteMonthlyActivity(ids);
        return success(true);
    }

    @GetMapping("/getMonthlyActivity")
    @Operation(summary = "获得月度活动详情")
    @Parameter(name = "activityId", description = "活动ID", required = true, example = "1024")
    @TenantIgnore
    public CommonResult<MonthlyActivityDO> getMonthlyActivity(@RequestParam("activityId") Long activityId) {
        MonthlyActivityDO monthlyActivity = monthlyActivityService.getMonthlyActivity(activityId);
        return success(monthlyActivity);
    }


    @GetMapping("/getMonthlyActivityByMonth")
    @Operation(summary = "根据月份查询所有活动")
    @TenantIgnore
    public CommonResult<List<MonthlyActivityDO>> getMonthlyActivityByMonth(@RequestParam("month") String month,
                                                                           @RequestParam(value = "tenantIds",required = false) Long[] tenantIds,
                                                                           @RequestParam(value = "orgType",required = false) Integer orgType) {
        tenantIds = tenantIds == null ? new Long[]{TenantContextHolder.getTenantId()} : tenantIds;
        return success(monthlyActivityService.getMonthlyActivityByMonth(month, tenantIds,orgType));
    }

    @GetMapping("/getMonthlyActivityPage")
    @Operation(summary = "获得月度活动分页列表")
    @TenantIgnore
    public CommonResult<PageResult<MonthlyActivityDO>> getMonthlyActivityPage(@Valid MonthlyActivityPageReqVO pageReqVO) {
        pageReqVO.setTenantIds(Objects.isNull(pageReqVO.getTenantIds()) ? new Long[]{TenantContextHolder.getTenantId()} : pageReqVO.getTenantIds());
        PageResult<MonthlyActivityDO> pageResult = monthlyActivityService.getMonthlyActivityPage(pageReqVO);
        for (MonthlyActivityDO monthlyActivityDO : pageResult.getList()) {
            monthlyActivityDO.setTenantName(tenantService.getTenant(monthlyActivityDO.getTenantId()).getName());
        }
        return success(pageResult);
    }


    @PostMapping("/createActivityExecutionRecord")
    @Operation(summary = "创建月度活动执行记录")
    @TenantIgnore
    public CommonResult<Boolean> createActivityExecutionRecord(@RequestBody ActivityExecutionRecordDO activityExecutionRecordDO) {
        activityExecutionRecordDO.setTenantId(activityExecutionRecordDO.getTenantId() == null ? TenantContextHolder.getTenantId() : activityExecutionRecordDO.getTenantId());
        monthlyActivityService.createActivityExecutionRecord(activityExecutionRecordDO);
        return success(true);
    }


    @GetMapping("/getActivityExecutionRecord")
    @Operation(summary = "获取月度活动执行计划记录详情")
    @Parameter(name = "activityId", description = "活动ID", required = true, example = "1024")
    @TenantIgnore
    public CommonResult<MonthlyActivityRespVO>  getActivityExecutionRecord(@RequestParam("activityId") Long activityId){
        return success(monthlyActivityService.getActivityExecutionRecord(activityId));
    }




    @GetMapping("/exportMonthlyActivity")
    @Operation(summary = "导出月度活动 Excel")
    @ApiAccessLog(operateType = EXPORT)
    public void exportMonthlyActivityExcel(@Valid MonthlyActivityPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<MonthlyActivityDO> list = monthlyActivityService.getMonthlyActivityPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "月度活动.xls", "数据", MonthlyActivityExcelVO.class,
                        BeanUtils.toBean(list, MonthlyActivityExcelVO.class));
    }

}