package cn.com.ctop.toutiao.modules.material.controller;

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

import cn.com.ctop.toutiao.modules.material.entity.ByteDanceAudiencePackage;
import cn.com.ctop.toutiao.modules.material.service.IByteDanceAudiencePackageService;
import cn.com.ctop.toutiao.modules.tool.util.JsonObjectToJsonStringUtil;
import cn.com.ctop.toutiao.modules.tool.util.JsonStringToJsonObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;

import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

 /**
 * 头条定向包
 */
@Slf4j
@RestController
@RequestMapping("/ctop/audiencePackage")
public class ByteDanceAudiencePackageController {

	@Autowired
	private IByteDanceAudiencePackageService audiencePackageService;

	/**
	 * 分页列表查询
	 */
	@GetMapping(value = "/list")
	public Result<IPage<ByteDanceAudiencePackage>> queryPageList(ByteDanceAudiencePackage byteDanceAudiencePackage,
																 @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
																 @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
																 HttpServletRequest req) {
		Result<IPage<ByteDanceAudiencePackage>> result = new Result<>();
		QueryWrapper<ByteDanceAudiencePackage> queryWrapper = QueryGenerator.initQueryWrapper(byteDanceAudiencePackage, req.getParameterMap());
		Page<ByteDanceAudiencePackage> page = new Page<>(pageNo, pageSize);
		IPage<ByteDanceAudiencePackage> pageList = audiencePackageService.page(page, queryWrapper);
		result.setSuccess(true);
		result.setResult(pageList);
		return result;
	}

	 /**
	  * 通过id查询
	  */
	 @GetMapping(value = "/queryById")
	 public Result<ByteDanceAudiencePackage> queryById(@RequestParam(name="id",required=true) Long id) {
		 Result<ByteDanceAudiencePackage> result = new Result<>();
		 ByteDanceAudiencePackage byteDanceAudiencePackage = JsonStringToJsonObject.toAudiencePackage(audiencePackageService.getById(id));
		 result.setResult(byteDanceAudiencePackage);
		 result.setSuccess(true);
		 return result;
	 }

	 /**
	  * 通过user_id查询
	  */
	 @GetMapping(value = "/queryByUserId")
	 public Result<IPage<ByteDanceAudiencePackage>> queryByUserId(@RequestParam(name="userId",required=true) String userId,
																 @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
																 @RequestParam(name="pageSize", defaultValue="10") Integer pageSize) {
		 Result<IPage<ByteDanceAudiencePackage>> result = new Result<>();
		 QueryWrapper<ByteDanceAudiencePackage> wrapper= new QueryWrapper<>();
		 wrapper.eq("user_id",userId);
		 Page<ByteDanceAudiencePackage> page = new Page<>(pageNo, pageSize);
		 IPage<ByteDanceAudiencePackage> byteDanceAudiencePackagePage = audiencePackageService.page(page,wrapper);
		 result.setResult(byteDanceAudiencePackagePage);
		 result.setSuccess(true);
		 return result;
	 }

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

	/**
	 *  编辑
	 */
	@PostMapping(value = "/edit")
	public Result<ByteDanceAudiencePackage> edit(@RequestBody ByteDanceAudiencePackage byteDanceAudiencePackage) {
		Result<ByteDanceAudiencePackage> result = new Result<>();
		ByteDanceAudiencePackage byteDanceAudiencePackageEntity = audiencePackageService.getById(byteDanceAudiencePackage.getId());
		if(byteDanceAudiencePackageEntity ==null) {
			result.error500("未找到数据");
		}else {
			boolean ok = audiencePackageService.updateById(JsonObjectToJsonStringUtil.toAudiencePackage(byteDanceAudiencePackage));
			if(ok) {
				result.success("修改成功!");
			}
		}
		return result;
	}

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

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

}
