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

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
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.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import lombok.extern.slf4j.Slf4j;

import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
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.vo.LoginUser;
import org.jeecg.modules.adweb.enquiry.entity.AdwebSiteEnquiryRule;
import org.jeecg.modules.adweb.enquiry.mapper.AdwebSiteEnquiryRuleMapper;
import org.jeecg.modules.adweb.enquiry.service.IAdwebSiteEnquiryRuleService;
import org.jeecg.modules.adweb.site.entity.AdwebSite;
import org.jeecg.modules.adweb.site.service.IAdwebSiteService;
import org.jeecg.modules.adweb.site.service.ISiteUserPermissionService;
import org.jeecg.modules.adweb.system.service.SysAdwebApi;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description: adweb_site_enquiry_rule
 * @Author: jeecg-boot
 * @Date: 2023-08-31
 * @Version: V1.0
 */
@Tag(name = "adweb_site_enquiry_rule")
@RestController
@RequestMapping("/adweb/adwebEnquirySiteRule")
@Slf4j
public class AdwebSiteEnquiryRuleController
        extends JeecgController<AdwebSiteEnquiryRule, IAdwebSiteEnquiryRuleService> {
    @Resource private IAdwebSiteEnquiryRuleService adwebSiteEnquiryRuleService;
    @Resource private AdwebSiteEnquiryRuleMapper adwebEnquirySiteRuleMapper;
    @Resource private IAdwebSiteService adwebSiteService;
    @Resource private SysAdwebApi sysAdwebApi;
    @Resource private ISiteUserPermissionService siteUserPermissionService;

    /**
     * 分页列表查询
     *
     * @param adwebEnquirySiteRule
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @AutoLog(value = "adweb_site_enquiry_rule-分页列表查询")
    @Operation(summary = "adweb_site_enquiry_rule-分页列表查询")
    @GetMapping(value = "/list")
    public Result<?> queryPageList(
            AdwebSiteEnquiryRule adwebEnquirySiteRule,
            @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
            @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
            HttpServletRequest req) {
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        List<String> codeList = null;
        List<String> uidList = null;
        if (sysAdwebApi.isAdmin()) {

        } else if (sysAdwebApi.isChannelAdmin()) {
            uidList = sysAdwebApi.getChannelGroupUids();
        } else {
            uidList = new ArrayList<>();
            uidList.add(sysUser.getId());
        }
        List<String> siteCodeList = null;
        if (CollectionUtils.isNotEmpty(uidList)) {
            siteCodeList = siteUserPermissionService.getSiteCodeListByUids(uidList);
        }

        if (adwebEnquirySiteRule.getSiteId() != null) {
            String siteCode = adwebSiteService.getSiteCodeById(adwebEnquirySiteRule.getSiteId());
            if (StringUtils.isNotBlank(siteCode)) {
                LambdaQueryWrapper<AdwebSite> queryWrapper = new LambdaQueryWrapper<>();
                queryWrapper
                        .eq(AdwebSite::getId, adwebEnquirySiteRule.getSiteId())
                        .or()
                        .eq(AdwebSite::getParentGroupCode, siteCode);
                queryWrapper.ne(AdwebSite::getStatus, 0);
                queryWrapper.in(!sysAdwebApi.isAdmin(), AdwebSite::getCode, siteCodeList);
                codeList =
                        adwebSiteService.list(queryWrapper).stream()
                                .map(AdwebSite::getCode)
                                .collect(Collectors.toList());
            }
        } else {
            codeList = siteCodeList;
        }
        Page<AdwebSiteEnquiryRule> page = new Page<AdwebSiteEnquiryRule>(pageNo, pageSize);
        IPage<AdwebSiteEnquiryRule> pageList = getPageList(page, adwebEnquirySiteRule, codeList);
        List<AdwebSiteEnquiryRule> record = pageList.getRecords();

        return Result.OK(pageList);
    }

    public IPage<AdwebSiteEnquiryRule> getPageList(
            Page<AdwebSiteEnquiryRule> page,
            AdwebSiteEnquiryRule adwebEnquirySiteRule,
            List<String> codeList) {

        IPage<AdwebSiteEnquiryRule> pageList =
                adwebEnquirySiteRuleMapper.pageList(page, adwebEnquirySiteRule, codeList);
        return pageList;
    }

    /**
     * 添加
     *
     * @param adwebEnquirySiteRule
     * @return
     */
    @AutoLog(value = "adweb_site_enquiry_rule-添加")
    @Operation(summary = "adweb_site_enquiry_rule-添加")
    @PostMapping(value = "/add")
    public Result<?> add(@RequestBody AdwebSiteEnquiryRule adwebEnquirySiteRule) {
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();

        AdwebSite adwebSite = adwebSiteService.getById(adwebEnquirySiteRule.getSiteId());
        adwebEnquirySiteRule.setSiteCode(adwebSite.getCode());
        adwebEnquirySiteRule.setStatus(1);
        adwebSiteEnquiryRuleService.save(adwebEnquirySiteRule);
        return Result.OK("添加成功！");
    }

    /**
     * 编辑
     *
     * @param adwebEnquirySiteRule
     * @return
     */
    @AutoLog(value = "adweb_site_enquiry_rule-编辑")
    @Operation(summary = "adweb_site_enquiry_rule-编辑")
    @PutMapping(value = "/edit")
    public Result<?> edit(@RequestBody AdwebSiteEnquiryRule adwebEnquirySiteRule) {
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();

        AdwebSite adwebSite = adwebSiteService.getById(adwebEnquirySiteRule.getSiteId());
        adwebEnquirySiteRule.setSiteCode(adwebSite.getCode());
        adwebSiteEnquiryRuleService.updateById(adwebEnquirySiteRule);
        return Result.OK("编辑成功!");
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "adweb_site_enquiry_rule-通过id删除")
    @Operation(summary = "adweb_site_enquiry_rule-通过id删除")
    @DeleteMapping(value = "/delete")
    public Result<?> delete(@RequestParam(name = "id", required = true) String id) {
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();

        AdwebSiteEnquiryRule adwebEnquirySiteRule = adwebSiteEnquiryRuleService.getById(id);
        adwebEnquirySiteRule.setStatus(0);
        adwebSiteEnquiryRuleService.updateById(adwebEnquirySiteRule);
        //		adwebSiteEnquiryRuleService.removeById(id);
        return Result.OK("删除成功!");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "adweb_site_enquiry_rule-批量删除")
    @Operation(summary = "adweb_site_enquiry_rule-批量删除")
    @DeleteMapping(value = "/deleteBatch")
    public Result<?> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();

        List<String> strings = Arrays.asList(ids.split(","));
        for (String s : strings) {
            AdwebSiteEnquiryRule adwebEnquirySiteRule = adwebSiteEnquiryRuleService.getById(s);
            adwebEnquirySiteRule.setStatus(0);
            adwebSiteEnquiryRuleService.updateById(adwebEnquirySiteRule);
        }
        //		this.adwebSiteEnquiryRuleService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.OK("批量删除成功!");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @AutoLog(value = "adweb_site_enquiry_rule-通过id查询")
    @Operation(summary = "adweb_site_enquiry_rule-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<?> queryById(@RequestParam(name = "id", required = true) String id) {
        AdwebSiteEnquiryRule adwebEnquirySiteRule = adwebSiteEnquiryRuleService.getById(id);
        if (adwebEnquirySiteRule == null) {
            return Result.error("未找到对应数据");
        }
        return Result.OK(adwebEnquirySiteRule);
    }

    /**
     * 导出excel
     *
     * @param request
     * @param adwebEnquirySiteRule
     */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(
            HttpServletRequest request, AdwebSiteEnquiryRule adwebEnquirySiteRule) {
        return super.exportXls(
                request,
                adwebEnquirySiteRule,
                AdwebSiteEnquiryRule.class,
                "adweb_site_enquiry_rule");
    }

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