package org.jeecg.modules.okki.site.controller;

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.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
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.okki.account.service.IOkkiAccountService;
import org.jeecg.modules.okki.site.entity.OkkiSite;
import org.jeecg.modules.okki.site.service.IOkkiSiteService;
import org.jeecg.modules.okki.siteuser.entity.OkkiSiteUser;
import org.jeecg.modules.okki.siteuser.service.IOkkiSiteUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.*;

/**
 * @Description: 站点
 * @Author: jeecg-boot
 * @Date: 2024-04-29
 * @Version: V1.0
 */
@Api(tags = "站点")
@RestController
@RequestMapping("/site/okkiSite")
@Slf4j
public class OkkiSiteController extends JeecgController<OkkiSite, IOkkiSiteService> {
    @Autowired
    private IOkkiSiteService okkiSiteService;

    @Autowired
    private IOkkiAccountService okkiAccountService;

    @Autowired
    private IOkkiSiteUserService okkiSiteUserService;

    /**
     * 分页列表查询
     *
     * @param okkiSite
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    //@AutoLog(value = "站点-分页列表查询")
    @ApiOperation(value = "站点-分页列表查询", notes = "站点-分页列表查询")
    @GetMapping(value = "/list")
    public Result<IPage<OkkiSite>> queryPageList(OkkiSite okkiSite,
                                                 @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                 @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                                 HttpServletRequest req) {
        // 初始化查询条件
        QueryWrapper<OkkiSite> queryWrapper = QueryGenerator.initQueryWrapper(okkiSite, req.getParameterMap());
        Page<OkkiSite> page = new Page<>(pageNo, pageSize);
//        queryWrapper.eq("del_flag", 2);
        IPage<OkkiSite> pageList = okkiSiteService.page(page, queryWrapper);

        // 处理 OkkiSite 列表
        List<OkkiSite> okkiSites = pageList.getRecords();
        if (!okkiSites.isEmpty()) {
            okkiSites.forEach(item -> {
                Map<String, Object> siteBindInfoMap = okkiAccountService.getSiteBindInfo(Integer.valueOf(item.getSiteId()));
//                log.info("siteBindInfoMap: {}", FastJsonUtil.toJSONString(siteBindInfoMap));
                okkiSiteService.setApiOkkiSiteUserStr(item, siteBindInfoMap, "1");
                okkiSiteService.setApiOkkiSiteUserStr(item, siteBindInfoMap, "5");
                okkiSiteService.setApiOkkiSiteUserStr(item, siteBindInfoMap, "2");
                okkiSiteService.setApiOkkiSiteUserStr(item, siteBindInfoMap, "7");
                okkiSiteService.setApiOkkiSiteUserStr(item, siteBindInfoMap, "9");
                okkiSiteService.setApiOkkiSiteUserStr(item, siteBindInfoMap, "10");
                okkiSiteService.setApiOkkiSiteUserStr(item, siteBindInfoMap, "4");
                okkiSiteService.setApiOkkiSiteUserStr(item, siteBindInfoMap, "11");
                okkiSiteService.setApiOkkiSiteUserStr(item, siteBindInfoMap, "8");
                okkiSiteService.setApiOkkiSiteUserStr(item, siteBindInfoMap, "12");

//                QueryWrapper<OkkiSiteUser> queryWrapper1 = new QueryWrapper<>();
//                queryWrapper1.eq("site_id",Integer.valueOf(item.getSiteId()));
//                List<OkkiSiteUser> okkiSiteUsers = okkiSiteUserService.getSiteUserInfoBySiteId(Integer.valueOf(item.getSiteId()));
//                log.info("okkiSiteUsers:{}",FastJsonUtil.toJSONString(okkiSiteUsers));
//
//                // 调用方法处理数据
//                Map<String, List<OkkiSiteUser>> result = groupByType(okkiSiteUsers);
//                log.info("result:{}",FastJsonUtil.toJSONString(result));
//
//                okkiSiteService.setOkkiSiteUserStr(item, result, "serviceManageUser");
//                okkiSiteService.setOkkiSiteUserStr(item, result, "kefuUser");
//                okkiSiteService.setOkkiSiteUserStr(item, result, "seoUser");
//                okkiSiteService.setOkkiSiteUserStr(item, result, "semManageUser");
//                okkiSiteService.setOkkiSiteUserStr(item, result, "dataUser");
//                okkiSiteService.setOkkiSiteUserStr(item, result, "semUser");
//                okkiSiteService.setOkkiSiteUserStr(item, result, "websiteUser");
//                okkiSiteService.setOkkiSiteUserStr(item, result, "uiUser");
//                okkiSiteService.setOkkiSiteUserStr(item, result, "socialUser");
//                okkiSiteService.setOkkiSiteUserStr(item, result, "rdUser");
            });
        }

        return Result.OK(pageList);
    }

    public static Map<String, List<OkkiSiteUser>> groupByType(List<OkkiSiteUser> okkiSiteUsers) {
        Map<String, List<OkkiSiteUser>> groupedData = new HashMap<>();

        for (int i = 0; i < okkiSiteUsers.size(); i++) {
            OkkiSiteUser okkiSiteUser = okkiSiteUsers.get(i);;
            String type = okkiSiteUser.getType();
            groupedData.putIfAbsent(type, new ArrayList<>());

            groupedData.get(type).add(okkiSiteUser);
        }
        return groupedData;
    }


    /**
     * 添加
     *
     * @param okkiSite
     * @return
     */
    @AutoLog(value = "站点-添加")
    @ApiOperation(value = "站点-添加", notes = "站点-添加")
    @RequiresPermissions("site:okki_site:add")
    @PostMapping(value = "/add")
    public Result<String> add(@RequestBody OkkiSite okkiSite) {
        okkiSiteService.save(okkiSite);
        return Result.OK("添加成功！");
    }

    /**
     * 编辑
     *
     * @param okkiSite
     * @return
     */
    @AutoLog(value = "站点-编辑")
    @ApiOperation(value = "站点-编辑", notes = "站点-编辑")
    @RequiresPermissions("site:okki_site:edit")
    @RequestMapping(value = "/edit", method = {RequestMethod.PUT, RequestMethod.POST})
    public Result<String> edit(@RequestBody OkkiSite okkiSite) {
        okkiSiteService.updateById(okkiSite);
        return Result.OK("编辑成功!");
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "站点-通过id删除")
    @ApiOperation(value = "站点-通过id删除", notes = "站点-通过id删除")
    @RequiresPermissions("site:okki_site:delete")
    @DeleteMapping(value = "/delete")
    public Result<String> delete(@RequestParam(name = "id", required = true) String id) {
        okkiSiteService.removeById(id);
        return Result.OK("删除成功!");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "站点-批量删除")
    @ApiOperation(value = "站点-批量删除", notes = "站点-批量删除")
    @RequiresPermissions("site:okki_site:deleteBatch")
    @DeleteMapping(value = "/deleteBatch")
    public Result<String> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        this.okkiSiteService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.OK("批量删除成功!");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    //@AutoLog(value = "站点-通过id查询")
    @ApiOperation(value = "站点-通过id查询", notes = "站点-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<OkkiSite> queryById(@RequestParam(name = "id", required = true) String id) {
        OkkiSite okkiSite = okkiSiteService.getById(id);
        if (okkiSite == null) {
            return Result.error("未找到对应数据");
        }
        return Result.OK(okkiSite);
    }

    /**
     * 导出excel
     *
     * @param request
     * @param okkiSite
     */
    @RequiresPermissions("site:okki_site:exportXls")
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, OkkiSite okkiSite) {
        return super.exportXls(request, okkiSite, OkkiSite.class, "站点");
    }

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

    /**
     * 变更状态
     *
     * @param okkiSite
     * @return
     */
    @PostMapping(value = "/status")
    public Result<String> changeStatus(@RequestBody OkkiSite okkiSite) throws Exception {
        boolean result = okkiSiteService.changeStatus(okkiSite);
        if (result) {
            return Result.OK("操作成功");
        } else {
            return Result.error("操作失败");
        }
    }
}
