package org.jeecg.modules.ctop.controller;

import cn.com.ctop.common.module.utils.QueryGenerator;
import cn.com.ctop.crawler.modules.music.entity.BytedanceMusicInfo;
import cn.com.ctop.crawler.modules.music.entity.BytedanceMusicType;
import cn.com.ctop.crawler.modules.music.service.IBytedanceMusicInfoService;
import cn.com.ctop.crawler.modules.music.service.IBytedanceMusicTypeService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 音乐类型信息
 * @author jeecg-boot
 * @date   2020-10-28
 * @version V1.0
 */
@Slf4j
@RestController
@RequestMapping("/ctop/bytedanceMusicType")
public class BytedanceMusicTypeController {
	@Autowired
	private IBytedanceMusicTypeService bytedanceMusicTypeService;
	@Autowired
	private IBytedanceMusicInfoService bytedanceMusicInfoService;

	/**
	  * 分页列表查询
	 * @param bytedanceMusicType
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@PostMapping(value = "/list")
	public Result<IPage<BytedanceMusicType>> queryPageList(BytedanceMusicType bytedanceMusicType,
														   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
														   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
														   HttpServletRequest req) {
		String typeValue = bytedanceMusicType.getTypeValue();
		String author = bytedanceMusicType.getAuthor();
		String musicName = bytedanceMusicType.getMusicName();
		bytedanceMusicType.setTypeValue(null);
		bytedanceMusicType.setAuthor(null);
		bytedanceMusicType.setMusicName(null);
		Result<IPage<BytedanceMusicType>> result = new Result<>();
		QueryWrapper<BytedanceMusicType> queryWrapper = QueryGenerator.initQueryWrapper(bytedanceMusicType, req.getParameterMap());
		if(null!=typeValue&&!"".equals(typeValue.trim())){
			queryWrapper.like("type_value",typeValue);
		}else {
			queryWrapper.select("DISTINCT music_id");
		}
		if(null!=author&&!"".equals(author.trim())){
			queryWrapper.like("author",author);
		}
		if(null!=musicName&&!"".equals(musicName.trim())){
			queryWrapper.like("music_name",musicName);
		}
		Page<BytedanceMusicType> page = new Page<>(pageNo, pageSize);
		IPage<BytedanceMusicType> pageList = bytedanceMusicTypeService.page(page, queryWrapper);
		List<BytedanceMusicType> records = pageList.getRecords();
		if(null!=records&&!records.isEmpty()){
			List<BytedanceMusicType> setRecords = new ArrayList<>();
			for (BytedanceMusicType type:records) {
				BytedanceMusicInfo musicInfo = bytedanceMusicInfoService.getById(type.getMusicId());
				if(null!=musicInfo){
					type.setMusicInfo(musicInfo);
				}
				setRecords.add(type);
			}
			pageList.setRecords(setRecords);
		}
		result.setSuccess(true);
		result.setResult(pageList);
		return result;
	}

	/**
	  *   添加
	 * @param bytedanceMusicType
	 * @return
	 */
	@PostMapping(value = "/add")
	public Result<BytedanceMusicType> add(@RequestBody BytedanceMusicType bytedanceMusicType) {
		Result<BytedanceMusicType> result = new Result<>();
		try {
			bytedanceMusicTypeService.save(bytedanceMusicType);
			result.success("添加成功！");
		} catch (Exception e) {
			log.error(e.getMessage(),e);
			result.error500("操作失败");
		}
		return result;
	}

	/**
	  *  编辑
	 * @param bytedanceMusicType
	 * @return
	 */
	@PutMapping(value = "/edit")
	public Result<BytedanceMusicType> edit(@RequestBody BytedanceMusicType bytedanceMusicType) {
		Result<BytedanceMusicType> result = new Result<BytedanceMusicType>();
		BytedanceMusicType bytedanceMusicTypeEntity = bytedanceMusicTypeService.getById(bytedanceMusicType.getId());
		if(bytedanceMusicTypeEntity==null) {
			result.error500("未找到对应实体");
		}else {
			boolean ok = bytedanceMusicTypeService.updateById(bytedanceMusicType);
			if(ok) {
				result.success("修改成功!");
			}
		}
		return result;
	}

	/**
	  *   通过id删除
	 * @param id
	 * @return
	 */
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name="id",required=true) String id) {
		try {
			bytedanceMusicTypeService.removeById(id);
		} catch (Exception e) {
			log.error("删除失败",e.getMessage());
			return Result.error("删除失败!");
		}
		return Result.ok("删除成功!");
	}

	/**
	  *  批量删除
	 * @param ids
	 * @return
	 */
	@DeleteMapping(value = "/deleteBatch")
	public Result<BytedanceMusicType> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		Result<BytedanceMusicType> result = new Result<>();
		if(ids==null || "".equals(ids.trim())) {
			result.error500("参数不识别！");
		}else {
			this.bytedanceMusicTypeService.removeByIds(Arrays.asList(ids.split(",")));
			result.success("删除成功!");
		}
		return result;
	}

	/**
	  * 通过id查询
	 * @param id
	 * @return
	 */
	@GetMapping(value = "/queryById")
	public Result<BytedanceMusicType> queryById(@RequestParam(name="id",required=true) String id) {
		Result<BytedanceMusicType> result = new Result<>();
		BytedanceMusicType bytedanceMusicType = bytedanceMusicTypeService.getById(id);
		if(bytedanceMusicType==null) {
			result.error500("未找到对应实体");
		}else {
			result.setResult(bytedanceMusicType);
			result.setSuccess(true);
		}
		return result;
	}
}
