package cn.com.ctop.kuaishou.modules.ai.controller;

import cn.com.ctop.common.module.entity.UserAllocation;
import cn.com.ctop.common.module.service.IUserAllocationService;
import cn.com.ctop.kuaishou.modules.ai.entity.AiKuaishouAdvertiserStrategy;
import cn.com.ctop.kuaishou.modules.ai.service.IAiKuaishouAdvertiserStrategyService;
import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
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   2021-01-25
 * @version V1.0
 */
@Slf4j
@RestController
@RequestMapping("/ctop/aiKuaishouAdvertiserStrategy")
public class AiKuaishouAdvertiserStrategyController {
	@Autowired
	private IAiKuaishouAdvertiserStrategyService aiKuaishouAdvertiserStrategyService;

	/**
	  * 分页列表查询
	 * @param aiKuaishouAdvertiserStrategy
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@ApiOperation(value="快手账户策略设置信息表-分页列表查询", notes="快手账户策略设置信息表-分页列表查询")
	@GetMapping(value = "/list")
	public Result<IPage<AiKuaishouAdvertiserStrategy>> queryPageList(AiKuaishouAdvertiserStrategy aiKuaishouAdvertiserStrategy,
																	 @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
																	 @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
																	 HttpServletRequest req) {
		Result<IPage<AiKuaishouAdvertiserStrategy>> result = new Result<>();
		QueryWrapper<AiKuaishouAdvertiserStrategy> queryWrapper = QueryGenerator.initQueryWrapper(aiKuaishouAdvertiserStrategy, req.getParameterMap());
		Page<AiKuaishouAdvertiserStrategy> page = new Page<>(pageNo, pageSize);
		IPage<AiKuaishouAdvertiserStrategy> pageList = aiKuaishouAdvertiserStrategyService.page(page, queryWrapper);
		List<AiKuaishouAdvertiserStrategy> records = pageList.getRecords();
		List<AiKuaishouAdvertiserStrategy> setData = new ArrayList<>();
		if(null!=records&&!records.isEmpty()){
			records.forEach(record->{
				Long accountId = record.getAccountId();
				UserAllocation allocation = allocationService.getByAccountId(accountId);
				if(null !=allocation){
					record.setAuthName(allocation.getAuthName());
				}
				setData.add(record);
			});
			pageList.setRecords(setData);
		}
		result.setSuccess(true);
		result.setResult(pageList);
		return result;
	}

	@Autowired
	private IUserAllocationService allocationService;

	/**
	 *   添加
	 * @param aiKuaishouAdvertiserStrategy
	 * @return
	 */
	@ApiOperation(value="快手账户策略设置信息表-添加", notes="快手账户策略设置信息表-添加")
	@PostMapping(value = "/add")
	public Result<AiKuaishouAdvertiserStrategy> add(@RequestBody AiKuaishouAdvertiserStrategy aiKuaishouAdvertiserStrategy) {
		Result<AiKuaishouAdvertiserStrategy> result = new Result<>();
		try {
			Integer singleAppId = aiKuaishouAdvertiserStrategy.getSingleAppid();
			if(null!=singleAppId&&singleAppId==1){
				Integer appId = Integer.parseInt(JSONArray.parseArray(aiKuaishouAdvertiserStrategy.getAppIdArray()).getString(0));
				aiKuaishouAdvertiserStrategy.setAppId(appId);
			}
			aiKuaishouAdvertiserStrategyService.save(aiKuaishouAdvertiserStrategy);
			result.success("添加成功！");
		} catch (Exception e) {
			log.error(e.getMessage(),e);
			result.error500("操作失败");
		}
		return result;
	}

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

		return result;
	}

	/**
	  *   通过id删除
	 * @param id
	 * @return
	 */
	@ApiOperation(value="快手账户策略设置信息表-通过id删除", notes="快手账户策略设置信息表-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name="id",required=true) String id) {
		try {
			aiKuaishouAdvertiserStrategyService.removeById(id);
		} catch (Exception e) {
			log.error("删除失败",e.getMessage());
			return Result.error("删除失败!");
		}
		return Result.ok("删除成功!");
	}

	/**
	  *  批量删除
	 * @param ids
	 * @return
	 */
	@ApiOperation(value="快手账户策略设置信息表-批量删除", notes="快手账户策略设置信息表-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<AiKuaishouAdvertiserStrategy> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		Result<AiKuaishouAdvertiserStrategy> result = new Result<>();
		if(ids==null || "".equals(ids.trim())) {
			result.error500("参数不识别！");
		}else {
			this.aiKuaishouAdvertiserStrategyService.removeByIds(Arrays.asList(ids.split(",")));
			result.success("删除成功!");
		}
		return result;
	}

	/**
	  * 通过id查询
	 * @param id
	 * @return
	 */
	@ApiOperation(value="快手账户策略设置信息表-通过id查询", notes="快手账户策略设置信息表-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<AiKuaishouAdvertiserStrategy> queryById(@RequestParam(name="id",required=true) String id) {
		Result<AiKuaishouAdvertiserStrategy> result = new Result<>();
		AiKuaishouAdvertiserStrategy aiKuaishouAdvertiserStrategy = aiKuaishouAdvertiserStrategyService.getById(id);
		if(aiKuaishouAdvertiserStrategy==null) {
			result.error500("未找到对应实体");
		}else {
			result.setResult(aiKuaishouAdvertiserStrategy);
			result.setSuccess(true);
		}
		return result;
	}
}
