package org.jeecg.modules.ctop.controller;

import cn.com.ctop.common.module.utils.ResultMapUtils;
import cn.com.ctop.common.module.utils.StatusCode;
import cn.com.ctop.kuaishou.modules.ai.entity.KuaishouAccountSpeedExploreLog;
import cn.com.ctop.kuaishou.modules.ai.service.IKuaishouAccountSpeedExploreLogService;
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.apache.shiro.SecurityUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.system.vo.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

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

/**
 * 快手账户加速探索触发记录信息
 * @author jeecg-boot
 * @date   2021-03-08
 * @version V1.0
 */
@Slf4j
@RestController
@RequestMapping("/ctop/kuaishouAccountSpeedExploreLog")
public class KuaishouAccountSpeedExploreLogController {
	@Autowired
	private IKuaishouAccountSpeedExploreLogService kuaishouAccountSpeedExploreLogService;

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

	/**
	 *   添加
	 * @param kuaishouAccountSpeedExploreLog
	 * @return
	 */
	@PostMapping(value = "/open")
	public Map<String,Object> add(@RequestBody KuaishouAccountSpeedExploreLog kuaishouAccountSpeedExploreLog) {
		Map<String,Object>result = new HashMap<>();
		try {
			LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();
			kuaishouAccountSpeedExploreLog.setUserId(user.getId());
			//1:查询此账户当前是否存在正在加速探索的账户
			if(null!=kuaishouAccountSpeedExploreLog.getConvertType()&&(kuaishouAccountSpeedExploreLog.getConvertType()==2||kuaishouAccountSpeedExploreLog.getConvertType()==180||kuaishouAccountSpeedExploreLog.getConvertType()==53||kuaishouAccountSpeedExploreLog.getConvertType()==190)){
				return kuaishouAccountSpeedExploreLogService.openSpeedExplore(kuaishouAccountSpeedExploreLog);
			}else {
				ResultMapUtils.setResultMap(result, StatusCode.KUAISHOU_SPEED_EXPLORE_ACTION_TYPE_NOT_CONFIG);
				return result;
			}
		} catch (Exception e) {
			log.error(e.getMessage(),e);
			ResultMapUtils.setResultMap(result, StatusCode.COMMON_SERVER_ERROR);
			return result;
		}
	}

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

		return result;
	}

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

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

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