package org.jeecg.modules.adweb.subscribePlan.controller;

import cn.hutool.core.util.RandomUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
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.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.modules.adweb.subscribePlan.entity.SubscribePlan;
import org.jeecg.modules.adweb.subscribePlan.service.ISubscribePlanService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

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

/**
 * @Description: 网站营销方案套餐表
 * @Author: jeecg-boot
 * @Date: 2024-09-27
 * @Version: V1.0
 */
@Tag(name = "网站营销方案套餐表")
@RestController
@RequestMapping("/adweb/subscribePlan")
@Slf4j
public class SubscribePlanController extends JeecgController<SubscribePlan, ISubscribePlanService> {
    @Autowired private ISubscribePlanService seoMarketPlanService;

    //    @Autowired
    //	private IAdwebMaterialCheckService checkService;

    /**
     * 分页列表查询
     *
     * @param subscribePlan
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    // @AutoLog(value = "网站营销方案套餐表-分页列表查询")
    @Operation(summary = "网站营销方案套餐表-分页列表查询")
    @GetMapping(value = "/list")
    public Result<IPage<SubscribePlan>> queryPageList(
            SubscribePlan subscribePlan,
            @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
            @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
            HttpServletRequest req) {
        QueryWrapper<SubscribePlan> queryWrapper =
                QueryGenerator.initQueryWrapper(subscribePlan, req.getParameterMap());
        Page<SubscribePlan> page = new Page<SubscribePlan>(pageNo, pageSize);
        IPage<SubscribePlan> pageList = seoMarketPlanService.page(page, queryWrapper);
        return Result.OK(pageList);
    }

    /**
     * 添加
     *
     * @param subscribePlan
     * @return
     */
    @AutoLog(value = "网站营销方案套餐表-添加")
    @Operation(summary = "网站营销方案套餐表-添加")
    @PreAuthorize("@jps.requiresPermissions('adweb:seo_market_plan:add')")
    @PostMapping(value = "/add")
    public Result<String> add(@RequestBody SubscribePlan subscribePlan) {

        subscribePlan.setCreateTime(new Date());
        LambdaQueryWrapper<SubscribePlan> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SubscribePlan::getMarketType, subscribePlan.getMarketType());
        queryWrapper.eq(SubscribePlan::getPlanName, subscribePlan.getPlanName());
        queryWrapper.ne(SubscribePlan::getStatus, 0);
        List<SubscribePlan> seoMarketPlans = seoMarketPlanService.list(queryWrapper);
        if (!seoMarketPlans.isEmpty()) {
            return Result.error("方案名称已存在！");
        }
        // 生成编码
        subscribePlan.setPlanCode(subscribePlan.getMarketType() + getPlanCode());
        if (subscribePlan.getKeywordCount() == null) {
            subscribePlan.setKeywordCount(0);
        }

        seoMarketPlanService.save(subscribePlan);
        return Result.OK("添加成功！");
    }

    /**
     * 编辑
     *
     * @param subscribePlan
     * @return
     */
    @AutoLog(value = "网站营销方案套餐表-编辑")
    @Operation(summary = "网站营销方案套餐表-编辑")
    @PreAuthorize("@jps.requiresPermissions('adweb:seo_market_plan:edit')")
    @RequestMapping(
            value = "/edit",
            method = {RequestMethod.PUT, RequestMethod.POST})
    public Result<String> edit(@RequestBody SubscribePlan subscribePlan) {
        seoMarketPlanService.updateById(subscribePlan);
        return Result.OK("编辑成功!");
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "网站营销方案套餐表-通过id删除")
    @Operation(summary = "网站营销方案套餐表-通过id删除")
    @PreAuthorize("@jps.requiresPermissions('adweb:seo_market_plan:delete')")
    @DeleteMapping(value = "/delete")
    public Result<String> delete(@RequestParam(name = "id", required = true) String id) {
        seoMarketPlanService.removeById(id);
        return Result.OK("删除成功!");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "网站营销方案套餐表-批量删除")
    @Operation(summary = "网站营销方案套餐表-批量删除")
    @PreAuthorize("@jps.requiresPermissions('adweb:seo_market_plan:deleteBatch')")
    @DeleteMapping(value = "/deleteBatch")
    public Result<String> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        this.seoMarketPlanService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.OK("批量删除成功!");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    // @AutoLog(value = "网站营销方案套餐表-通过id查询")
    @Operation(summary = "网站营销方案套餐表-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<SubscribePlan> queryById(@RequestParam(name = "id", required = true) String id) {
        SubscribePlan subscribePlan = seoMarketPlanService.getById(id);
        if (subscribePlan == null) {
            return Result.error("未找到对应数据");
        }
        return Result.OK(subscribePlan);
    }

    /**
     * 导出excel
     *
     * @param request
     * @param subscribePlan
     */
    @PreAuthorize("@jps.requiresPermissions('adweb:seo_market_plan:exportXls')")
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, SubscribePlan subscribePlan) {
        return super.exportXls(request, subscribePlan, SubscribePlan.class, "网站营销方案套餐表");
    }

    /**
     * 通过excel导入数据
     *
     * @param request
     * @param response
     * @return
     */
    @PreAuthorize("@jps.requiresPermissions('adweb:seo_market_plan:importExcel')")
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, SubscribePlan.class);
    }

    /** 获取随机字符串作为planCode @Author: luxiaoxiao @Date: 2021/11/16 */
    private String getPlanCode() {
        String planCode = RandomUtil.randomStringUpper(4);
        planCode = planCode.toUpperCase();
        LambdaQueryWrapper<SubscribePlan> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SubscribePlan::getPlanCode, planCode);
        int count = (int) seoMarketPlanService.count(queryWrapper);
        return count > 0 ? getPlanCode() : planCode;
    }

    //	/**
    //	 * 物料完整性检测模板设置
    //	 *
    //	 * @Author:yangjingwen
    //	 * @Date:2022/5/7
    //	 */
    //	@GetMapping(value="/integrityChecking")
    //	public Result<?> integrityChecking(String planId){
    //		QueryWrapper<AdwebMaterialCheck> queryWrapper=new QueryWrapper<>();
    //		queryWrapper.eq("plan_id",planId).orderByAsc("sort");
    //		List<AdwebMaterialCheck> checkList = checkService.list(queryWrapper);
    //		if(!checkList.isEmpty()){
    //			return Result.OK(checkList);
    //		}
    //		//查出模板数据
    //		QueryWrapper<AdwebMaterialCheck> queryWrapper1 = new QueryWrapper<>();
    //		queryWrapper1.isNull("plan_id").orderByAsc("sort");
    //		List<AdwebMaterialCheck> checkTemplateList = checkService.list(queryWrapper1);
    //		checkTemplateList.forEach(check -> {
    //			check.setPlanId(planId);
    //		});
    //		checkService.saveBatch(checkTemplateList);
    //		return Result.OK(checkTemplateList);
    //	}

}
