package com.yonge.cooleshow.tenant.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.webportal.exception.BizException;
import com.yonge.cooleshow.auth.api.client.SysUserFeignService;
import com.yonge.cooleshow.auth.api.entity.SysUser;
import com.yonge.cooleshow.biz.dal.entity.TenantAlbumPurchase;
import com.yonge.cooleshow.biz.dal.entity.TenantInfo;
import com.yonge.cooleshow.biz.dal.entity.TenantStaff;
import com.yonge.cooleshow.biz.dal.service.TenantAlbumPurchaseService;
import com.yonge.cooleshow.biz.dal.service.TenantInfoService;
import com.yonge.cooleshow.biz.dal.service.TenantStaffService;
import com.yonge.cooleshow.biz.dal.wrapper.TenantAlbumPurchaseWrapper;
import com.yonge.cooleshow.common.controller.BaseController;
import com.yonge.cooleshow.common.entity.HttpResponseResult;
import com.yonge.cooleshow.tenant.vo.TenantAlbumPurchaseVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
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;


@Slf4j
@Validated
@RestController
@RequestMapping("${app-config.url.tenant:}/tenantAlbumPurchase")
@Api(tags = "机构专辑采购")
public class TenantAlbumPurchaseController extends BaseController {

    @Autowired
    private TenantAlbumPurchaseService tenantAlbumPurchaseService;

    @Autowired
    private SysUserFeignService sysUserFeignService;

    @Autowired
    private TenantInfoService tenantInfoService;
    @Autowired
    private TenantStaffService tenantStaffService;

    @ApiOperation(value = "详情", notes = "机构专辑采购-根据详情ID查询单条, 传入id")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", dataType = "long")
    })
//    @PreAuthorize("@auditsvc.hasPermissions('tenantAlbumPurchase/detail', {'BACKEND'})")
//    @GetMapping("/detail/{id}")
    public HttpResponseResult<TenantAlbumPurchaseWrapper.TenantAlbumPurchase> detail(@PathVariable("id") Long id) {

        TenantAlbumPurchase wrapper = tenantAlbumPurchaseService.detail(id);

        return succeed(TenantAlbumPurchaseWrapper.TenantAlbumPurchase.from(JSON.toJSONString(wrapper)));
    }

    @ApiOperation(value = "查询分页", notes = "机构专辑采购- 传入 TenantAlbumPurchaseVo.TenantAlbumPurchaseQuery")
//    @PreAuthorize("@auditsvc.hasPermissions('tenantAlbumPurchase/page', {'BACKEND'})")
    @PostMapping("/page")
    public HttpResponseResult<PageInfo<TenantAlbumPurchaseWrapper.TenantAlbumPurchase>> page(
            @RequestBody TenantAlbumPurchaseWrapper.TenantAlbumPurchaseQuery query) {
        TenantInfo tenantInfo = getTenantInfo();
        query.setTenantId(tenantInfo.getId());
        // 查询数据
        IPage<TenantAlbumPurchaseWrapper.TenantAlbumPurchase> pages =
                tenantAlbumPurchaseService.selectPage(QueryInfo.getPage(query), query);
        return succeed(QueryInfo.pageInfo(pages, pages.getRecords()));
    }

    @ApiOperation(value = "新增", notes = "机构专辑采购- 传入 TenantAlbumPurchaseVo.TenantAlbumPurchase")
//    @PreAuthorize("@auditsvc.hasPermissions('tenantAlbumPurchase/save', {'BACKEND'})")
//    @PostMapping("/save")
    public HttpResponseResult<JSONObject> add(
            @Validated @RequestBody TenantAlbumPurchaseVo.TenantAlbumPurchase tenantAlbumPurchaseVo) {

        // 新增数据
        tenantAlbumPurchaseService.save(JSON.parseObject(tenantAlbumPurchaseVo.jsonString(),
                TenantAlbumPurchase.class));
        return succeed();
    }

    @ApiOperation(value = "修改", notes = "机构专辑采购- 传入 TenantAlbumPurchaseVo.TenantAlbumPurchase")
//    @PreAuthorize("@auditsvc.hasPermissions('tenantAlbumPurchase/update', {'BACKEND'})")
//    @PostMapping("/update")
    public HttpResponseResult<JSONObject> update(@Validated @RequestBody TenantAlbumPurchaseVo.TenantAlbumPurchase tenantAlbumPurchaseVo
    ) {
        // 更新数据
        tenantAlbumPurchaseService.updateById(JSON.parseObject(tenantAlbumPurchaseVo.jsonString(),
                TenantAlbumPurchase.class));
        return succeed();
    }

    @ApiOperation(value = "删除", notes = "机构专辑采购- 传入id")
//    @PreAuthorize("@auditsvc.hasPermissions('tenantAlbumPurchase/remove', {'BACKEND'})")
//    @PostMapping("/remove")
    public HttpResponseResult<Boolean> remove(@RequestParam Long id) {

        return succeed(tenantAlbumPurchaseService.removeById(id));
    }

    private TenantInfo getTenantInfo() {
        SysUser sysUser = sysUserFeignService.queryUserInfo();
        TenantStaff tenantStaff = tenantStaffService.getByUserId(sysUser.getId());
        if (tenantStaff == null) {
            throw new BizException("非法请求");
        }
        TenantInfo tenantInfo = tenantInfoService.getById(sysUser.getTenantId());
        if (tenantInfo == null) {
            throw new BizException("非法请求");
        }
        return tenantInfo;
    }
}