package org.jeecg.modules.demo.adsaccount.controller;

import java.util.Arrays;
import java.util.Date;

import dm.jdbc.util.StringUtil;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.modules.demo.adsaccount.entity.AdsAccount;
import org.jeecg.modules.demo.adsaccount.service.IAdsAccountService;

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.system.base.controller.JeecgController;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.apache.shiro.authz.annotation.RequiresPermissions;

 /**
 * @Description: 广告账户管理
 * @Author: jeecg-boot
 * @Date:   2024-09-20
 * @Version: V1.0
 */
@Tag(name="广告账户管理")
@RestController
@RequestMapping("/adsaccount/adsAccount")
@Slf4j
public class AdsAccountController extends JeecgController<AdsAccount, IAdsAccountService> {
	@Autowired
	private IAdsAccountService adsAccountService;

	 /**
	 * 分页列表查询
	 *
	 * @param adsAccount
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	//@AutoLog(value = "广告账户管理-分页列表查询")
	@Operation(summary="广告账户管理-分页列表查询")
	@GetMapping(value = "/list")
	public Result<IPage<AdsAccount>> queryPageList(AdsAccount adsAccount,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<AdsAccount> queryWrapper = QueryGenerator.initQueryWrapper(adsAccount, req.getParameterMap());
		Page<AdsAccount> page = new Page<AdsAccount>(pageNo, pageSize);
		IPage<AdsAccount> pageList = adsAccountService.page(page, queryWrapper);
		return Result.OK(pageList);
	}
	
	/**
	 *   添加
	 *
	 * @param adsAccount
	 * @return
	 */
	@AutoLog(value = "广告账户管理-添加")
	@Operation(summary="广告账户管理-添加")
	@RequiresPermissions("adsaccount:ads_account:add")
	@PostMapping(value = "/add")
	public Result<String> add(@RequestBody AdsAccount adsAccount) {
		adsAccount.setCreatedAt(new Date());
		adsAccountService.save(adsAccount);
		return Result.OK("添加成功！");
	}
	
	/**
	 *  编辑
	 *
	 * @param adsAccount
	 * @return
	 */
	@AutoLog(value = "广告账户管理-编辑")
	@Operation(summary="广告账户管理-编辑")
	@RequiresPermissions("adsaccount:ads_account:edit")
	@RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
	public Result<String> edit(@RequestBody AdsAccount adsAccount) {
		adsAccount.setUpdatedAt(new Date());
		adsAccountService.updateById(adsAccount);
		return Result.OK("编辑成功!");
	}
	
	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "广告账户管理-通过id删除")
	@Operation(summary="广告账户管理-通过id删除")
	@RequiresPermissions("adsaccount:ads_account:delete")
	@DeleteMapping(value = "/delete")
	public Result<String> delete(@RequestParam(name="id",required=true) String id) {
		adsAccountService.removeById(id);
		return Result.OK("删除成功!");
	}
	
	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "广告账户管理-批量删除")
	@Operation(summary="广告账户管理-批量删除")
	@RequiresPermissions("adsaccount:ads_account:deleteBatch")
	@DeleteMapping(value = "/deleteBatch")
	public Result<String> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.adsAccountService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	//@AutoLog(value = "广告账户管理-通过id查询")
	@Operation(summary="广告账户管理-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<AdsAccount> queryById(@RequestParam(name="id",required=true) String id) {
		AdsAccount adsAccount = adsAccountService.getById(id);
		if(adsAccount==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(adsAccount);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param adsAccount
    */
    @RequiresPermissions("adsaccount:ads_account:exportXls")
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, AdsAccount adsAccount) {
        return super.exportXls(request, adsAccount, AdsAccount.class, "广告账户管理");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    @RequiresPermissions("adsaccount:ads_account:importExcel")
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, AdsAccount.class);
    }

	 /**
	  *  审核
	  *
	  * @param adsAccount
	  * @return
	  */
	 @AutoLog(value = "广告账户管理-审核")
	 @Operation(summary="广告账户管理-审核")
	 @RequiresPermissions("adsaccount:ads_account:audit")
	 @RequestMapping(value = "/audit", method = {RequestMethod.PUT,RequestMethod.POST})
	 public Result<String> audit(@RequestBody AdsAccount adsAccount) {
		 if(StringUtil.equals(adsAccount.getReviewStatus(),"APPROVED") && adsAccount.getAccountId().matches(".*[a-zA-Z].*")){
			 String accountId =  adsAccountService.createGoogleAdsCustomer(adsAccount);
			 if(!StringUtil.equals(accountId,"false")){
				 adsAccount.setAccountId(accountId);
			 } else {
				 return Result.OK("创建谷歌广告账户失败!");
			 }
		 }
		 adsAccount.setUpdatedAt(new Date());
		 adsAccountService.updateById(adsAccount);
		 return Result.OK("审核成功!");
	 }

}
