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

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils;
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.ElderlyInfoPageReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.biz.ElderlyInfoDO;
import cn.iocoder.yudao.module.system.dal.dataobject.biz.ElderlyPhotoDO;
import cn.iocoder.yudao.module.system.service.biz.ElderlyPhotoService;
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.validation.Valid;

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

import java.util.List;
import java.util.Objects;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;


@Tag(name = "管理后台 - 长者相册")
@RestController
@RequestMapping("/elderly/photo")
@Validated
public class ElderlyPhotoController {

    @Resource
    private ElderlyPhotoService photoService;

    @PostMapping("/createPhotos")
    @Operation(summary = "创建长者相册")
    @TenantIgnore
    public CommonResult<Boolean> createPhotos(@Valid @RequestBody List<ElderlyPhotoDO> elderlyPhotoList) {
        photoService.createPhotos(elderlyPhotoList);
        return success(true);
    }

    @PutMapping("/update")
    @Operation(summary = "更新长者相册")
    @TenantIgnore
    public CommonResult<Boolean> updatePhotos(@Valid @RequestBody List<ElderlyPhotoDO> elderlyPhotoList) {
        photoService.updatePhotos(elderlyPhotoList);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除长者相册")
    @Parameter(name = "id", description = "编号", required = true)
    @TenantIgnore
    public CommonResult<Boolean> deletePhoto(@RequestParam("id") Long id) {
        photoService.deletePhoto(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得长者相册")
    @Parameter(name = "elderId", description = "长者ID", required = true, example = "1024")
    @TenantIgnore
    public CommonResult<List<ElderlyPhotoDO>> getPhoto(@RequestParam("elderId") Long elderId, @RequestParam(value = "type", required = false) Integer type) {
        return success(photoService.getPhoto(elderId, type));
    }


    @GetMapping("/findElderlyPhotoPage")
    @Operation(summary = "长者相册列表分页")
    @TenantIgnore
    public CommonResult<PageResult<ElderlyInfoDO>> findElderlyPhotoPage(@Valid ElderlyInfoPageReqVO pageVO) {
        pageVO.setTenantIds(Objects.isNull(pageVO.getTenantIds()) ? new Long[]{TenantContextHolder.getTenantId()} : pageVO.getTenantIds());
        PageResult<ElderlyInfoDO> pageResult = photoService.findElderlyPhotoPage(pageVO);
        return success(BeanUtils.toBean(pageResult, ElderlyInfoDO.class));
    }
//
//    @GetMapping("/page")
//    @Operation(summary = "获得长者相册分页")
//    @PreAuthorize("@ss.hasPermission('elderly:photo:query')")
//    public CommonResult<PageResult<PhotoRespVO>> getPhotoPage(@Valid PhotoPageReqVO pageReqVO) {
//        PageResult<PhotoDO> pageResult = photoService.getPhotoPage(pageReqVO);
//        return success(BeanUtils.toBean(pageResult, PhotoRespVO.class));
//    }
//
//    @GetMapping("/export-excel")
//    @Operation(summary = "导出长者相册 Excel")
//    @PreAuthorize("@ss.hasPermission('elderly:photo:export')")
//    @ApiAccessLog(operateType = EXPORT)
//    public void exportPhotoExcel(@Valid PhotoPageReqVO pageReqVO,
//              HttpServletResponse response) throws IOException {
//        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
//        List<PhotoDO> list = photoService.getPhotoPage(pageReqVO).getList();
//        // 导出 Excel
//        ExcelUtils.write(response, "长者相册.xls", "数据", PhotoRespVO.class,
//                        BeanUtils.toBean(list, PhotoRespVO.class));
//    }

}