package com.yonge.cooleshow.student.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
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.Student;
import com.yonge.cooleshow.biz.dal.entity.TenantInfo;
import com.yonge.cooleshow.biz.dal.entity.UserTenantAlbumRecord;
import com.yonge.cooleshow.biz.dal.service.StudentService;
import com.yonge.cooleshow.biz.dal.service.TenantInfoService;
import com.yonge.cooleshow.biz.dal.service.UserTenantAlbumRecordService;
import com.yonge.cooleshow.biz.dal.wrapper.TenantAlbumWrapper;
import com.yonge.cooleshow.biz.dal.wrapper.UserTenantAlbumRecordWrapper;
import com.yonge.cooleshow.common.entity.HttpResponseResult;
import com.yonge.toolset.base.exception.BizException;
import com.yonge.toolset.base.page.PageInfo;
import com.yonge.toolset.mybatis.support.PageUtil;
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.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Optional;

@Slf4j
@Validated
@RestController
@RequestMapping("${app-config.url.student:}/userTenantAlbumRecord")
@Api(tags = "购买训练工具记录")
public class UserTenantAlbumRecordController {
    @Autowired
    private UserTenantAlbumRecordService userTenantAlbumRecordService;

    @Resource
    private SysUserFeignService sysUserFeignService;

    @Autowired
    private TenantInfoService tenantInfoService;

    @Autowired
    private StudentService studentService;

    @ApiOperation(value = "详情", notes = "购买训练工具记录-根据详情ID查询单条, 传入id")
    @PreAuthorize("@auditsvc.hasPermissions('userTenantAlbumRecord/detail', {'BACKEND'})")
    //@GetMapping("/detail/{id}")
    public R<UserTenantAlbumRecord> detail(@PathVariable("id") Long id) {

        UserTenantAlbumRecord wrapper = userTenantAlbumRecordService.detail(id);

        return R.from(wrapper);
    }

    @ApiOperation(value = "查询分页", notes = "购买训练工具记录- 传入 UserTenantAlbumRecordWrapper.UserTenantAlbumRecordQuery")
    //@PreAuthorize("@auditsvc.hasPermissions('userTenantAlbumRecord/page', {'BACKEND'})")
    @PostMapping("/page")
    public HttpResponseResult<PageInfo<TenantAlbumWrapper.TenantAlbum>> page(@RequestBody UserTenantAlbumRecordWrapper.UserTenantAlbumRecordQuery query) {

        IPage<TenantAlbumWrapper.TenantAlbum> pages = userTenantAlbumRecordService.selectPage(QueryInfo.getPage(query), query);

        return HttpResponseResult.succeed(PageUtil.pageInfo(pages));
    }

    @ApiOperation(value = "学生机构专辑列表查询", notes = "学生机构专辑列表查询")
    @PostMapping("/list")
    public R<List<UserTenantAlbumRecordWrapper.UserTenantAlbumRecord>> list(@RequestBody UserTenantAlbumRecordWrapper.UserTenantAlbumRecordQuery query) {

        SysUser sysUser = sysUserFeignService.queryUserInfo();
        Student student = studentService.getById(sysUser.getId());
        if (student == null || student.getTenantId() == null || student.getTenantId() == -1L) {
            throw new BizException("请求非法");
        }
        Long tenantGroupId = student.getTenantGroupId();
        if (tenantGroupId == null || tenantGroupId == -1L) {
            return R.from(new ArrayList<>());
        }
        query.setUserId(sysUser.getId());
        query.setEndTime(new Date());
        query.setTenantId(student.getTenantId());
        query.setTenantGroupId(student.getTenantGroupId());
        return R.from(userTenantAlbumRecordService.list(query));
    }


    @ApiOperation(value = "查询详情")
    @PostMapping("/detail")
    public HttpResponseResult <TenantAlbumWrapper.TenantAlbum> Albumdetail(@RequestParam(defaultValue = "") String albumId) {
        TenantAlbumWrapper.TenantAlbum tenantAlbum= userTenantAlbumRecordService.detailAlbum(albumId);
        return HttpResponseResult.succeed(tenantAlbum);
    }





    @ApiOperation(value = "新增", notes = "购买训练工具记录- 传入 UserTenantAlbumRecordWrapper.UserTenantAlbumRecord")
    @PreAuthorize("@auditsvc.hasPermissions('userTenantAlbumRecord/save', {'BACKEND'})")
    //@PostMapping("/save")
    public R<JSONObject> add(@Validated @RequestBody UserTenantAlbumRecord userTenantAlbumRecord) {

        // 新增数据
        userTenantAlbumRecordService.save(userTenantAlbumRecord);

        return R.defaultR();
    }

    @ApiOperation(value = "修改", notes = "购买训练工具记录- 传入 UserTenantAlbumRecordWrapper.UserTenantAlbumRecord")
    @PreAuthorize("@auditsvc.hasPermissions('userTenantAlbumRecord/update', {'BACKEND'})")
    //@PostMapping("/update")
    public R<JSONObject> update(@Validated @RequestBody UserTenantAlbumRecord userTenantAlbumRecord) {

        // 更新数据
        userTenantAlbumRecordService.updateById(userTenantAlbumRecord);

        return R.defaultR();
    }

    @ApiOperation(value = "删除", notes = "购买训练工具记录- 传入id")
    @PreAuthorize("@auditsvc.hasPermissions('userTenantAlbumRecord/remove', {'BACKEND'})")
    //@PostMapping("/remove")
    public R<Boolean> remove(@RequestParam Long id) {

        return R.from(userTenantAlbumRecordService.removeById(id));
    }
}