package com.yonge.cooleshow.admin.controller;

import com.alibaba.fastjson.JSONObject;
import com.dayaedu.cbs.openfeign.client.MusicFeignClientService;
import com.dayaedu.cbs.openfeign.properties.OpenFeignClientConfigProperties;
import com.dayaedu.cbs.openfeign.service.CbsMusicSheetService;
import com.dayaedu.cbs.openfeign.wrapper.music.*;
import com.microsvc.toolkit.common.response.paging.PageInfo;
import com.microsvc.toolkit.common.response.template.R;
import com.microsvc.toolkit.common.webportal.exception.BizException;
import com.microsvc.toolkit.config.jwt.utils.JwtUserInfo;
import com.yonge.cooleshow.biz.dal.enums.SubjectTypeEnum;
import com.yonge.cooleshow.biz.dal.service.MusicSheetService;
import com.yonge.cooleshow.biz.dal.service.SysConfigService;
import com.yonge.cooleshow.biz.dal.wrapper.MusicSheetWrapper;
import com.yonge.cooleshow.common.constant.SysConfigConstant;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
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 springfox.documentation.annotations.ApiIgnore;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

@Slf4j
@Validated
@RestController
@RequestMapping("${app-config.url.admin:}/tenantMusicSheetApplicationExtend")
@Api(tags = "曲目应用拓展表")
public class TenantMusicSheetApplicationExtendController {

    @Autowired
    private MusicFeignClientService musicFeignClientService;

    @Autowired
    private MusicSheetService musicSheetService;

    @Autowired
    private SysConfigService sysConfigService;

    @PreAuthorize("@pcs.hasPermissions('tenantMusicSheetApplicationExtend/pageByApplication')")
    @PostMapping({"/pageByApplication"})
    public R<PageInfo<MusicSheetWrapper.MusicSheetApplication>> musicSheetPageByApplication(@RequestBody MusicSheetWrapper.MusicSheetApplicationQuery var1){

        return R.from(musicSheetService.tenantPage(var1));
    }


    @PreAuthorize("@pcs.hasPermissions('tenantMusicSheetApplicationExtend/page')")
    @PostMapping({"/page"})
    public R<PageInfo<MusicSheetWrapper.CbsMusicSheet>> musicSheetPage(@RequestBody CbsMusicSheetWrapper.MusicSheetQuery var1){

        String cbsTenantAppId = sysConfigService.findConfigValue(SysConfigConstant.CBS_TENANT_APP_ID);
        // 更新数据
        var1.setAddAppId(Long.parseLong(cbsTenantAppId));
        try {
            PageInfo<CbsMusicSheetWrapper.MusicSheet> musicSheetPageInfo = this.musicFeignClientService.musicSheetPage(var1).feignData();
            PageInfo<MusicSheetWrapper.CbsMusicSheet> convert = musicSheetPageInfo.convert(o -> JSONObject.parseObject(JSONObject.toJSONString(o), MusicSheetWrapper.CbsMusicSheet.class));

            return R.from(convert);
        }catch (Exception e) {
            log.error("查询曲目应用拓展表异常", e);
            if(e instanceof BizException) {
                throw e;
            }
            throw BizException.from("内容平台服务异常");
        }
    }

    @ApiOperation(value = "修改应用扩展信息", notes = "曲目应用拓展表- 传入 MusicSheetApplicationExtendWrapper.MusicSheetApplicationExtend")
    @PreAuthorize("@pcs.hasPermissions('tenantMusicSheetApplicationExtend/update')")
	@PostMapping("/update")
	public R<JSONObject> update( @RequestBody CbsMusicSheetApplicationExtendClientWrapper.AddMusicSheetApplicationExtend addMusicSheetApplicationExtend) {

        String cbsTenantAppId = sysConfigService.findConfigValue(SysConfigConstant.CBS_TENANT_APP_ID);
        // 更新数据
        addMusicSheetApplicationExtend.setApplicationId(Long.parseLong(cbsTenantAppId));
        try {

            musicFeignClientService.musicSheetApplicationExtendUpdate(addMusicSheetApplicationExtend).feignData();
        }catch (Exception e) {
            log.error("修改应用扩展信息异常", e);
            if(e instanceof BizException) {
                throw e;
            }
            throw BizException.from("内容平台服务异常");
        }

        return R.defaultR();
	}


    @ApiOperation(value = "启/停用", notes = "曲目应用拓展表- 传入 MusicSheetApplicationExtendWrapper.MusicSheetApplicationExtend")
    @PreAuthorize("@pcs.hasPermissions('tenantMusicSheetApplicationExtend/status')")
    @PostMapping("/status")
    public R<JSONObject> status(@Validated @RequestBody CbsMusicSheetApplicationExtendClientWrapper.MusicSheetApplicationExtendStatus status) {

        // 更新数据
        try {

            musicFeignClientService.musicSheetApplicationExtendStatus(status).feignData();
        }catch (Exception e) {
            log.error("启/停用异常", e);
            if(e instanceof BizException) {
                throw e;
            }
            throw BizException.from("内容平台服务异常");
        }

        return R.defaultR();
    }

    @ApiOperation(value = "批量保存扩展信息")
    @PreAuthorize("@pcs.hasPermissions('tenantMusicSheetApplicationExtend/saveBatch', {'BACKEND'})")
    @PostMapping("/saveBatch")
    public R<JSONObject> saveBatch( @RequestBody List<CbsMusicSheetApplicationExtendClientWrapper.AddMusicSheetApplicationExtend> list) {

        String cbsTenantAppId = sysConfigService.findConfigValue(SysConfigConstant.CBS_TENANT_APP_ID);
        if (CollectionUtils.isEmpty(list)) {
            throw BizException.from("参数错误");
        }
        for (CbsMusicSheetApplicationExtendClientWrapper.AddMusicSheetApplicationExtend addMusicSheetApplicationExtend : list) {
            addMusicSheetApplicationExtend.setApplicationId(Long.parseLong(cbsTenantAppId));
        }
        // 更新数据
        try {

            musicFeignClientService.musicSheetApplicationExtendSaveBatch(list).feignData();
        }catch (Exception e) {
            log.error("批量保存扩展信息异常", e);
            if(e instanceof BizException) {
                throw e;
            }
            throw BizException.from("内容平台服务异常");
        }

        return R.defaultR();
    }


    @ApiOperation(value = "应用分类列表")
    @PostMapping("/categoryList")
    public R<List<MusicSheetCategoriesWrapper.MusicSheetCategories>> categoryList(String type) {

        String cbsTenantAppId = sysConfigService.findConfigValue(SysConfigConstant.CBS_TENANT_APP_ID);

        return R.from(musicFeignClientService.queryTreeByAppId(Long.parseLong(cbsTenantAppId)).feignData());

    }

    @ApiOperation(value = "删除应用扩展信息", notes = "曲目应用拓展表- 传入 MusicSheetApplicationExtendWrapper.MusicSheetApplicationExtend")
    @PreAuthorize("@pcs.hasPermissions('tenantMusicSheetApplicationExtend/del')")
    @PostMapping("/del/{id}")
    public R<JSONObject> update(@PathVariable Long id) {

        // 更新数据
        return R.from(musicFeignClientService.musicSheetApplicationExtendDel(id).feignData());

    }

}
