package com.yonge.cooleshow.admin.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yonge.cooleshow.admin.io.request.ActivityTeacherVO;
import com.yonge.cooleshow.auth.api.client.SysUserFeignService;
import com.yonge.cooleshow.auth.api.entity.SysUser;
import com.yonge.cooleshow.biz.dal.dto.ActivityEvaluationDto;
import com.yonge.cooleshow.biz.dal.dto.activity.ActivityTeacherQuery;
import com.yonge.cooleshow.biz.dal.dto.search.ActivityEvaluationSearch;
import com.yonge.cooleshow.biz.dal.dto.search.MusicSheetSearch;
import com.yonge.cooleshow.biz.dal.enums.AuthStatusEnum;
import com.yonge.cooleshow.biz.dal.service.ActivityEvaluationService;
import com.yonge.cooleshow.biz.dal.vo.ActivityEvaluationVo;
import com.yonge.cooleshow.biz.dal.vo.MusicSheetVo;
import com.yonge.cooleshow.biz.dal.vo.activity.ActivityTeacherWrapper;
import com.yonge.cooleshow.common.controller.BaseController;
import com.yonge.cooleshow.common.entity.HttpResponseResult;
import com.yonge.cooleshow.common.enums.YesOrNoEnum;
import com.yonge.toolset.base.page.PageInfo;
import com.yonge.toolset.base.util.StringUtil;
import com.yonge.toolset.mybatis.support.PageUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.collections.CollectionUtils;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.security.access.prepost.PreAuthorize;
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 javax.validation.Valid;
import java.util.List;
import java.util.Objects;
import java.util.Optional;

@RestController
@RequestMapping("/activityEvaluation")
@Api(value = "评测活动表", tags = "评测活动表")
public class ActivityEvaluationController extends BaseController {

    @Autowired
    private ActivityEvaluationService activityEvaluationService;
	@Resource
	private SysUserFeignService sysUserFeignService;

    /**
     * 查询分页
     */
    @PostMapping("/page")
    @ApiOperation(value = "查询分页", notes = "传入activityEvaluationSearch")
	@PreAuthorize("@pcs.hasPermissions('activityEvaluation/page')")
	public HttpResponseResult<PageInfo<ActivityEvaluationVo>> page(@RequestBody ActivityEvaluationSearch query) {
		IPage<ActivityEvaluationVo> pages = activityEvaluationService.selectPage(PageUtil.getPage(query), query);
        return succeed(PageUtil.pageInfo(pages));
	}

	@PostMapping("/list")
	@ApiOperation(value = "查询列表", notes = "传入activityEvaluationSearch")
	@PreAuthorize("@pcs.hasPermissions('activityEvaluation/list')")
	public HttpResponseResult<List<ActivityEvaluationVo>> selectAll(@RequestBody ActivityEvaluationSearch query) {
		List<ActivityEvaluationVo> list = activityEvaluationService.selectAll(query);
		return succeed(list);
	}
    
    /**
	 * 批量增加曲目
	 */
	@PostMapping("/addBatch")
	@ApiOperation(value = "批量增加曲目")
	@PreAuthorize("@pcs.hasPermissions('activityEvaluation/addBatch')")
	public HttpResponseResult addBatch(@Valid @RequestBody ActivityEvaluationDto param) {
		SysUser user = sysUserFeignService.queryUserInfo();
		if (user == null || null == user.getId()) {
			return failed(HttpStatus.FORBIDDEN, "请登录");
		}
    	return status(activityEvaluationService.addBatch(param,user));
	}

	@ApiOperation(value = "批量更新曲目信息")
	@PostMapping("/batchUpdate")
	public HttpResponseResult<Boolean> batchUpdateEvaluationInfo(@Valid @RequestBody ActivityEvaluationDto info) {

		SysUser user = sysUserFeignService.queryUserInfo();
		if (user == null || null == user.getId()) {
			return failed(HttpStatus.FORBIDDEN, "请登录");
		}

		if (CollectionUtils.isEmpty(info.getEvaluationInfos())) {
			return failed("评测曲目信息为空");
		}

		for (ActivityEvaluationDto.EvaluationInfo item : info.getEvaluationInfos()) {

			if (Objects.isNull(item.getId())
					|| Objects.isNull(item.getEvaluationDifficulty())) {
				return failed("无效的请求参数");
			}

			item.updateBy(user.getId()).updateTime(DateTime.now().toDate());
		}

		// 批量更新曲目信息
		int ret = activityEvaluationService.batchUpdateEvaluationInfo(user, info);

		return status(ret > 0);
	}

 	/**
	 * 删除
	 */
	@PostMapping("/remove")
	@ApiOperation(value = "删除", notes = "传入ids")
	@PreAuthorize("@pcs.hasPermissions('activityEvaluation/remove')")
	public HttpResponseResult remove(
			@ApiParam(value = "主键集合", required = true)
			@RequestParam(value = "ids") String ids) {
        if (StringUtil.isEmpty(ids)) {
			return failed("参数不能为空");
		}
		return status(activityEvaluationService.removeByIds(StringUtil.toLongList(ids)));
	}


	@ApiOperation(value = "分页查询曲目", httpMethod="POST", consumes="application/json", produces="application/json")
	@PostMapping(value="/musicPage", consumes="application/json", produces="application/json")
	@PreAuthorize("@pcs.hasPermissions('activityEvaluation/musicPage')")
	public HttpResponseResult<PageInfo<MusicSheetVo>> musicPage(@RequestBody MusicSheetSearch query) {
		if(null == query.getActivityId()){
			return failed("活动id不能为空");
		}
		query.setAuditStatus(AuthStatusEnum.PASS);
		query.setState(YesOrNoEnum.YES);
		query.setAuditVersion(YesOrNoEnum.NO);
		IPage<MusicSheetVo> musicSheetVoIPage = activityEvaluationService.musicPage(PageUtil.getPage(query), query);
		return succeed(PageUtil.pageInfo(musicSheetVoIPage));
	}

	/**
	 * 分享活动老师信息
	 * @param request ActivityTeacherInfoRequest
	 * @return HttpResponseResult<PageInfo<ActivityTeacherResponse>>
	 */
	@ApiOperation(value = "分页查询活动老师信息", httpMethod="POST", consumes="application/json", produces="application/json")
	@PostMapping(value="/teachers", consumes="application/json", produces="application/json")
	public HttpResponseResult<PageInfo<ActivityTeacherVO.ResponseInfo>> activityTeacherPageInfo(@RequestBody ActivityTeacherVO.RequestInfo request) {

		if(Objects.isNull(request.getActivityId())){
			return failed("活动id不能为空");
		}

		// 老师信息
		IPage<ActivityTeacherWrapper> wrapper = activityEvaluationService.activityTeacherPageInfo(PageUtil.getPage(request),
				ActivityTeacherQuery.from(request.jsonString()));

		// 数据转换
		List<ActivityTeacherVO.ResponseInfo> responses = JSON.parseArray(JSON.toJSONString(wrapper.getRecords()),
				ActivityTeacherVO.ResponseInfo.class);

		// 分页数据信息
		return succeed(PageUtil.getPageInfo(wrapper, responses));
	}
}