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

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.com.ctop.common.module.utils.Check;
import cn.com.ctop.kuaishou.modules.v3.entity.V3KuaishouAccountAutoTarget;
import cn.com.ctop.kuaishou.modules.v3.service.V3KuaishouAccountAutoTargetService;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.util.oConvertUtils;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;

import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.fastjson.JSON;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 自动投放-v3-本地定向表
 *
 * @author jeecg-boot
 * @version V1.0
 * @date 2022-10-31
 */
@Slf4j
@Api(tags = "自动投放-v3-本地定向表")
@RestController
@RequestMapping("/auto/v3KuaishouAccountAutoTarget")
public class V3KuaishouAccountAutoTargetController {
    @Autowired
    private V3KuaishouAccountAutoTargetService v3KuaishouAccountAutoTargetService;

    /**
     * 分页列表查询
     *
     * @param v3KuaishouAccountAutoTarget
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @ApiOperation(value = "自动投放-v3-本地定向表-分页列表查询", notes = "自动投放-v3-本地定向表-分页列表查询")
    @GetMapping(value = "/list")
    public Result<Object> queryPageList(Long accountId, String targetName,Integer platform, @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
																	@RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize) {
		try {
			if (Check.isNull(accountId)||Check.isNull(platform)) {
				return Result.error("缺少参数");
			}
			return v3KuaishouAccountAutoTargetService.queryPageList(accountId, targetName,platform, pageNo, pageSize);
		} catch (Exception e) {
			log.error("查询异常,", e.getMessage());
			e.printStackTrace();
		}
		return Result.error("查询失败");
    }
	/**
	 * 创建定向包
	 *
	 * @param
	 * @return
	 */
	@PostMapping(value = "/add")
	public Result<Object> add(@RequestBody JSONObject requestJson) {
		try {
			if (Check.isNull(requestJson.getString("targetName"))) {
				return Result.error("定向包名称为必传项");
			}
			Long accountId = requestJson.getLong("accountId");
			if (Check.isNull(accountId)) {
				return Result.error("请选择账户id");
			}
			return v3KuaishouAccountAutoTargetService.addV3LocalTarget(requestJson);
		} catch (Exception e) {
			log.error("创建定向包失败", e);
			return Result.error("创建失败" + e.getMessage());
		}
	}

	/**
	 * 修改定向包
	 *
	 * @param
	 * @return
	 */
	@PostMapping(value = "/edit")
	public Result<Object> edit(@RequestBody JSONObject requestJson) {
		try {
			if (Check.isNull(requestJson.getString("id"))) {
				return Result.error("请选择定向包");
			}
			V3KuaishouAccountAutoTarget target = v3KuaishouAccountAutoTargetService.getById(requestJson.getString("id"));
			if (target == null) {
				return Result.ok("修改失败，未查到对应定向包");
			}
			return v3KuaishouAccountAutoTargetService.updateV3LocalTarget(requestJson);
		} catch (Exception e) {
			return Result.error("修改异常");
		}
	}

	@PostMapping(value = "/getV3AutoTargetByIds")
	public Result<Object> getAutoTargetByIds(@RequestBody JSONObject data) {
		try {
			JSONArray ids = data.getJSONArray("ids");
			if (Check.isNull(ids)) {
				return Result.error("缺少参数");
			}
			List<String> list = JSONObject.parseObject(JSONObject.toJSONString(ids), List.class);
			if (Check.isNull(list)) {
				return Result.error("缺少参数");
			}
			List<V3KuaishouAccountAutoTarget> targets = v3KuaishouAccountAutoTargetService.getAutoTargetByIds(list);
			return Result.ok(targets);
		} catch (Exception e) {
			log.error("查询异常，", e);
			return Result.error("查询失败，" + e.getMessage());
		}
	}


    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "自动投放-v3-本地定向表-通过id删除", notes = "自动投放-v3-本地定向表-通过id删除")
    @GetMapping(value = "/delete")
    public Result<?> delete(@RequestParam(name = "id") String id) {
        try {
            v3KuaishouAccountAutoTargetService.removeById(id);
        } catch (Exception e) {
            log.error("删除失败", e.getMessage());
            return Result.error("删除失败!");
        }
        return Result.ok("删除成功!");
    }


    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "自动投放-v3-本地定向表-通过id查询", notes = "自动投放-v3-本地定向表-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<V3KuaishouAccountAutoTarget> queryById(@RequestParam(name = "id", required = true) String id) {
        Result<V3KuaishouAccountAutoTarget> result = new Result<>();
        V3KuaishouAccountAutoTarget v3KuaishouAccountAutoTarget = v3KuaishouAccountAutoTargetService.getById(id);
        if (v3KuaishouAccountAutoTarget == null) {
            result.error500("未找到对应实体");
        } else {
            result.setResult(v3KuaishouAccountAutoTarget);
            result.setSuccess(true);
        }
        return result;
    }
}
