package com.zcloud.controller.onlinexxks;

import com.zcloud.controller.base.BaseController;
import com.zcloud.entity.Page;
import com.zcloud.entity.PageData;
import com.zcloud.service.onlinexxks.CoursewareRelationService;
import com.zcloud.service.onlinexxks.VideoCoursewareService;
import com.zcloud.service.onlinexxks.VideocoursewareRecordService;
import com.zcloud.util.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.*;

/**
 * 说明：在线学习管理平台显示
 * 作者：guoyuepeng
 * 时间：2022年3月9日15:39:51
 * 官网：www.zcloudchina.com
 */
@Controller
@RequestMapping("/purchase")
public class PurchaseController extends BaseController {

	@Autowired
	private VideoCoursewareService videocoursewareService;
	@Autowired
	private VideocoursewareRecordService videocoursewareRecordService;
	@Autowired
	private CoursewareRelationService coursewarerelationService; // 课件与字典的关系表
	/**列表  企业端获取视频课件列表，
	 * 1.进行脱敏处理
	 * 2.接口加密
	 * @param page
	 * @throws Exception
	 */
	@RequestMapping(value="/getVideoCoursewareList" ,produces="application/json;charset=UTF-8")
	@ResponseBody
	public Object getVideoCoursewareList(Page page) throws Exception{
		Map<String,Object> map = new HashMap<String,Object>();
		String errInfo = "success";
		PageData pd = new PageData();
		pd = this.getPageData();
		String KEYWORDS = pd.getString("KEYWORDS");						//关键词检索条件
		if(Tools.notEmpty(KEYWORDS))pd.put("KEYWORDS", KEYWORDS.trim());
		pd.put("CORPINFO_ID",  pd.getString("corpInfoID"));

		Integer showCount = Integer.parseInt(pd.getString("showCount")); // 分页
		Integer currentPage = Integer.parseInt(pd.getString("currentPage"));

		page.setShowCount(showCount);
		page.setCurrentPage(currentPage);
		pd.put("STATE","0"); //只查询启用的课件
		page.setPd(pd);
		List<PageData>	varList = videocoursewareService.listByCorpinfo(page);	//列出VideoCourseware列表
		map.put("varList", varList);
		map.put("page", page);
		map.put("result", errInfo);
		return map;
	}
	/**列表  企业端点击添加课件的时候
	 * 1.查询id是否正确
	 * 2。查看是否后这个课件。
	 *   这个课件是否为0元
	 * 3.这个课件是否买过
	 * 4.整理好这个课件的数据返回个企业端
	 * @param page
	 * @throws Exception
	 */
	@RequestMapping(value="/joinMyCourseware")
	@ResponseBody
	public Object purchaseCourseware(Page page) throws Exception{
		Map<String,Object> map = new HashMap<String,Object>();
		String errInfo = "success";
		PageData pd = new PageData();
		pd = this.getPageData();

		pd.put("VIDEOCOURSEWARE_ID", pd.getString("coursewareID"));
		PageData pageData = new PageData();
		pageData = videocoursewareService.findById(pd);
		Double PRICE = 1d;
		if(!Tools.isEmpty(pageData.get("PRICE"))){
			PRICE = Double.parseDouble(pageData.get("PRICE").toString());
		}

		if(0d != PRICE){
			errInfo = "failure";
			map.put("pd", "");
			map.put("msg", "添加失败！该产品暂时不能添加！");
			map.put("result", errInfo);
		}

		/** 添加企业购买记录 开始 **/
		PageData record = new PageData();
		record.put("RECORD_ID",this.get32UUID());
		record.put("VIDEOCOURSEWARE_ID",pageData.get("VIDEOCOURSEWARE_ID"));
		record.put("CORPINFO_ID",pd.get("corpInfoID"));
		record.put("CREATOR",pd.get("userID"));
		record.put("CREATTIME",DateUtil.date2Str(new Date()));
		record.put("OPERATOR","admin");
		record.put("OPERATTIME",DateUtil.date2Str(new Date()));
		videocoursewareRecordService.save(record);
		/** 添加企业购买记录 结束 **/


		map.put("pd", pageData);
		map.put("result", errInfo);
		return map;
	}
}
