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.TenantAlbum;
import com.yonge.cooleshow.biz.dal.entity.TenantInfo;
import com.yonge.cooleshow.biz.dal.service.MusicAlbumService;
import com.yonge.cooleshow.biz.dal.service.TenantAlbumService;
import com.yonge.cooleshow.biz.dal.service.TenantInfoService;
import com.yonge.cooleshow.biz.dal.wrapper.TenantAlbumWrapper;
import com.yonge.cooleshow.common.controller.BaseController;
import com.yonge.cooleshow.common.entity.HttpResponseResult;
import com.yonge.cooleshow.tenant.vo.TenantAlbumVo;
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;

import java.util.List;

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

    @Autowired
    private TenantAlbumService tenantAlbumService;

    @Autowired
    private SysUserFeignService sysUserFeignService;

    @Autowired
    private TenantInfoService tenantInfoService;

    @Autowired
    private MusicAlbumService musicAlbumService;

    @ApiOperation(value = "详情", notes = "机构专辑-根据详情ID查询单条, 传入id")
//    @GetMapping("/detail/{id}")
    public HttpResponseResult<TenantAlbumVo.TenantAlbum> detail(@PathVariable("id") Long id) {
        TenantAlbum wrapper = tenantAlbumService.detail(id);
        return succeed(TenantAlbumVo.TenantAlbum.from(JSON.toJSONString(wrapper)));
    }

    @ApiOperation(value = "查询分页", notes = "机构专辑- 传入 TenantAlbumVo.TenantAlbumQuery")
    @PostMapping("/page")
    public HttpResponseResult<PageInfo<TenantAlbumVo.TenantAlbum>> page(@RequestBody TenantAlbumWrapper.TenantAlbumQuery query) {
        TenantInfo tenantInfo = getTenantInfo();
        query.setTenantId(tenantInfo.getId());
        // 查询数据
        IPage<TenantAlbumWrapper.TenantAlbum> pages = tenantAlbumService.selectPage(QueryInfo.getPage(query), query);
        // 数据类型转换
        List<TenantAlbumVo.TenantAlbum> records = JSON.parseArray(JSON.toJSONString(pages.getRecords()),
                TenantAlbumVo.TenantAlbum.class);
        return succeed(QueryInfo.pageInfo(pages, records));
    }

    @ApiOperation(value = "采购专辑", notes = "机构专辑- 传入 TenantAlbumVo.TenantAlbumQuery")
    @PostMapping("/buyAlbum")
    public HttpResponseResult<Boolean> buyAlbum(@RequestBody TenantAlbumWrapper.TenantAlbumBuy buy) {
        // todo 订单接入
        return succeed();
    }

    @ApiOperation(value = "新增", notes = "机构专辑- 传入 TenantAlbumVo.TenantAlbum")
//    @PostMapping("/add")
    public HttpResponseResult<JSONObject> add(@Validated @RequestBody TenantAlbumVo.TenantAlbum tenantAlbumVo) {

        // 新增数据
        tenantAlbumService.save(JSON.parseObject(tenantAlbumVo.jsonString(), TenantAlbum.class));
        return succeed();
    }

    @ApiOperation(value = "修改", notes = "机构专辑- 传入 TenantAlbumVo.TenantAlbum")
//    @PostMapping("/update")
    public HttpResponseResult<JSONObject> update(@Validated @RequestBody TenantAlbumVo.TenantAlbum tenantAlbumVo) {

        // 更新数据
        tenantAlbumService.updateById(JSON.parseObject(tenantAlbumVo.jsonString(), TenantAlbum.class));
        return succeed();
    }

    @ApiOperation(value = "删除", notes = "机构专辑- 传入id")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", dataType = "long")
    })
//    @PreAuthorize("@auditsvc.hasPermissions('tenantAlbum/remove', {'BACKEND'})")
//    @PostMapping("/remove")
    public HttpResponseResult<Boolean> remove(@RequestParam Long id) {
        return HttpResponseResult.succeed(tenantAlbumService.removeById(id));
    }

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


    @ApiOperation(value = "小程序端查询机构专辑详情")
    @PostMapping("/albumDetail")
    public HttpResponseResult <TenantAlbumWrapper.TenantAlbum> albumDetail(@RequestParam(defaultValue = "") String albumId) {
        TenantAlbumWrapper.TenantAlbum tenantAlbum= musicAlbumService.tenantDetailAlbum(albumId);
        return HttpResponseResult.succeed(tenantAlbum);
    }

}
