package cn.iocoder.yudao.module.member.controller.admin.activitytable;

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.operatelog.core.annotations.OperateLog;
import cn.iocoder.yudao.module.member.controller.admin.activitytable.vo.*;
import cn.iocoder.yudao.module.member.dal.dataobject.activitymerchant.ActivityMerchantDO;
import cn.iocoder.yudao.module.member.dal.dataobject.activitytable.ActivityTableDO;
import cn.iocoder.yudao.module.member.dal.dataobject.activitytabledish.ActivityTableDishDO;
import cn.iocoder.yudao.module.member.service.activitymerchant.ActivityMerchantService;
import cn.iocoder.yudao.module.member.service.activitytable.ActivityTableService;
import cn.iocoder.yudao.module.member.service.activitytabledish.ActivityTableDishService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;
import java.util.stream.Collectors;

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

@Tag(name = "管理后台 - 活动用餐桌子")
@RestController
@RequestMapping("/member/activity-table")
@Validated
public class ActivityTableController {

    @Resource
    private ActivityTableService activityTableService;
    @Resource
    private ActivityTableDishService activityTableDishService;
    @Resource
    private ActivityMerchantService activityMerchantService;

    @PostMapping("/create")
    @Operation(summary = "创建活动用餐桌子")
    @PreAuthorize("@ss.hasPermission('member:activity-table:create')")
    public CommonResult<Long> createActivityTable(@Valid @RequestBody ActivityTableSaveReqVO createReqVO) {
        return success(activityTableService.createActivityTable(createReqVO));
    }

    @PostMapping("/createFalse")
    @Operation(summary = "创建活动用餐桌子")
    @PreAuthorize("@ss.hasPermission('member:activity-table:create')")
    public CommonResult<Long> createActivityTable(@Valid @RequestBody ActivityTableCreateVO createReqVO) {
        return success(activityTableService.createActivityTableFalse(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新活动用餐桌子")
    @PreAuthorize("@ss.hasPermission('member:activity-table:update')")
    public CommonResult<Boolean> updateActivityTable(@Valid @RequestBody ActivityTableSaveReqVO updateReqVO) {
        activityTableService.updateActivityTable(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除活动用餐桌子")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('member:activity-table:delete')")
    public CommonResult<Boolean> deleteActivityTable(@RequestParam("id") Long id) {
        activityTableService.deleteActivityTable(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得活动用餐桌子")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('member:activity-table:query')")
    public CommonResult<ActivityTableRespVO> getActivityTable(@RequestParam("id") Long id) {
        ActivityTableDO activityTable = activityTableService.getActivityTable(id);
        return success(BeanUtils.toBean(activityTable, ActivityTableRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得活动用餐桌子分页")
    @PreAuthorize("@ss.hasPermission('member:activity-table:query')")
    public CommonResult<PageResult<ActivityTableRespVO>> getActivityTablePage(@Valid ActivityTablePageReqVO pageReqVO) {
        if(pageReqVO.getMerchantId() != null){
            List<ActivityTableDishDO> listByActivityIdAndMerchantId = activityTableDishService.getListByActivityIdAndMerchantId(pageReqVO.getActivityId(), pageReqVO.getMerchantId());
            if (listByActivityIdAndMerchantId.isEmpty()) {
                return success(PageResult.empty());
            }
             List<Integer> collect = listByActivityIdAndMerchantId.stream().map(ActivityTableDishDO::getSerialNumber).collect(Collectors.toList());
            pageReqVO.setSerialNumbers(collect);
        }


        PageResult<ActivityTableDO> pageResult = activityTableService.getActivityTablePage(pageReqVO);
        PageResult<ActivityTableRespVO> bean = BeanUtils.toBean(pageResult, ActivityTableRespVO.class);
        bean.getList().forEach(item -> {
            Long merchantIdByActivityIdAndSerialNumber = activityTableDishService.getMerchantIdByActivityIdAndSerialNumber(item.getActivityId(), item.getSerialNumber());
            if(merchantIdByActivityIdAndSerialNumber != null){
                ActivityMerchantDO activityMerchant = activityMerchantService.getActivityMerchant(merchantIdByActivityIdAndSerialNumber);
                item.setName(activityMerchant.getName());
            }
        });
        return success(bean);
    }

    @GetMapping("/list")
    @Operation(summary = "获得所有活动用餐桌子")
    @PreAuthorize("@ss.hasPermission('member:activity-table:query')")
    public CommonResult<List<ActivityTableRespVO>> getActivityTableList(@Valid ActivityTablePageReqVO pageReqVO) {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ActivityTableDO> list = activityTableService.getActivityTablePage(pageReqVO).getList();
        return success(BeanUtils.toBean(list, ActivityTableRespVO.class));
    }


    @GetMapping("/export-excel")
    @Operation(summary = "导出活动用餐桌子 Excel")
    @PreAuthorize("@ss.hasPermission('member:activity-table:export')")
    @OperateLog(type = EXPORT)
    public void exportActivityTableExcel(@Valid ActivityTablePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ActivityTableDO> list = activityTableService.getActivityTablePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "活动用餐桌子.xls", "数据", ActivityTableRespVO.class,
                        BeanUtils.toBean(list, ActivityTableRespVO.class));
    }

    @PutMapping("/updateTime")
    @Operation(summary = "修改用餐时间")
    public CommonResult<Boolean> updateTime(@Valid @RequestBody UpdateTimeVO updateTimeVO) {
        return success( activityTableService.updateTime(updateTimeVO));
    }

}