package com.yonge.cooleshow.admin.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.microsvc.toolkit.common.response.paging.PageInfo;
import com.microsvc.toolkit.common.response.paging.QueryInfo;
import com.microsvc.toolkit.common.response.template.R;
import com.yonge.cooleshow.auth.api.client.SysUserFeignService;
import com.yonge.cooleshow.auth.api.entity.SysUser;
import com.yonge.cooleshow.biz.dal.entity.TenantGroupAlbum;
import com.yonge.cooleshow.biz.dal.enums.PeriodEnum;
import com.yonge.cooleshow.biz.dal.service.TenantGroupAlbumService;
import com.yonge.cooleshow.biz.dal.wrapper.TenantGroupAlbumWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@Validated
@RestController
@RequestMapping("${app-config.url.admin:}/tenantGroupAlbum")
@Api(tags = "机构小组专辑配置表")
public class TenantGroupAlbumController {

    @Autowired
    private TenantGroupAlbumService tenantGroupAlbumService;

    @Resource
    private SysUserFeignService sysUserFeignService;

    @ApiOperation(value = "详情", notes = "机构小组专辑配置表-根据详情ID查询单条, 传入id")
    @PreAuthorize("@pcs.hasPermissions('tenantGroupAlbum/detail', {'BACKEND'})")
    //@GetMapping("/detail/{id}")
    public R<TenantGroupAlbum> detail(@PathVariable("id") Long id) {

        TenantGroupAlbum wrapper = tenantGroupAlbumService.detail(id);

        return R.from(wrapper);
    }

    @ApiOperation(value = "查询分页", notes = "机构小组专辑配置表- 传入 TenantGroupAlbumWrapper.TenantGroupAlbumQuery")
    @PreAuthorize("@pcs.hasPermissions('tenantGroupAlbum/page', {'BACKEND'})")
    @PostMapping("/page")
    public R<PageInfo<TenantGroupAlbumWrapper.TenantGroupAlbumVo>> page(@RequestBody TenantGroupAlbumWrapper.TenantGroupAlbumQuery query) {

        IPage<TenantGroupAlbumWrapper.TenantGroupAlbumVo> pages = tenantGroupAlbumService.selectPage(QueryInfo.getPage(query), query);

        return R.from(QueryInfo.pageInfo(pages));
    }

    @ApiOperation(value = "新增", notes = "机构小组专辑配置表- 传入 TenantGroupAlbumWrapper.TenantGroupAlbum")
    @PreAuthorize("@pcs.hasPermissions('tenantGroupAlbum/save', {'BACKEND'})")
    @PostMapping("/save")
    public R<JSONObject> add(@Validated @RequestBody TenantGroupAlbumWrapper.TenantGroupAlbum tenantGroupAlbum) {
        SysUser sysUser = sysUserFeignService.queryUserInfo();
        // 新增数据
        TenantGroupAlbum record = JSON.parseObject(JSON.toJSONString(tenantGroupAlbum), TenantGroupAlbum.class);
        record.setPurchasePeriod(PeriodEnum.DAY.getCode());
        record.setCreateBy(sysUser.getId());
        record.setUpdateBy(sysUser.getId());
        record.setStatus(false);
        record.setDelFlag(false);
        tenantGroupAlbumService.save(record);

        return R.defaultR();
    }

    @ApiOperation(value = "修改", notes = "机构小组专辑配置表- 传入 TenantGroupAlbumWrapper.TenantGroupAlbum")
    @PreAuthorize("@pcs.hasPermissions('tenantGroupAlbum/update', {'BACKEND'})")
    @PostMapping("/update")
    public R<JSONObject> update(@Validated @RequestBody TenantGroupAlbumWrapper.TenantGroupAlbum tenantGroupAlbum) {
        SysUser sysUser = sysUserFeignService.queryUserInfo();

        TenantGroupAlbum record = JSON.parseObject(JSON.toJSONString(tenantGroupAlbum), TenantGroupAlbum.class);
        record.setUpdateBy(sysUser.getId());
        // 更新数据
        tenantGroupAlbumService.updateById(record);

        return R.defaultR();
    }

    @ApiOperation(value = "删除", notes = "机构小组专辑配置表- 传入id")
    @PreAuthorize("@pcs.hasPermissions('tenantGroupAlbum/remove', {'BACKEND'})")
    @PostMapping("/remove")
    public R<Boolean> remove(@Validated @RequestBody TenantGroupAlbumWrapper.Remove remove) {
        tenantGroupAlbumService.lambdaUpdate()
                .set(TenantGroupAlbum::getDelFlag, true)
                .eq(TenantGroupAlbum::getId, remove.getId())
                .update();
        return R.from(true);
    }

    @ApiOperation(value = "删除", notes = "机构小组专辑配置表- 传入id")
    @PreAuthorize("@pcs.hasPermissions('tenantGroupAlbum/updateStatus', {'BACKEND'})")
    @PostMapping("/updateStatus")
    public R<Boolean> updateStatus(@RequestBody TenantGroupAlbumWrapper.UpdateStatus updateStatus) {
        tenantGroupAlbumService.updateStatus(updateStatus);
        return R.from(true);
    }
}