package com.flossom.system.controller;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.nacos.common.utils.CollectionUtils;
import com.flossom.common.core.domain.entity.SysTag;
import com.flossom.common.core.domain.R;
import com.flossom.common.core.domain.entity.WxMode;
import com.flossom.common.core.domain.entity.WxModeTag;
import com.flossom.common.core.enums.TagTypeStatusEnum;
import com.flossom.common.core.utils.poi.ExcelUtil;
import com.flossom.common.core.web.controller.BaseController;
import com.flossom.common.core.web.domain.AjaxResult;
import com.flossom.common.core.web.page.TableDataInfo;
import com.flossom.common.log.annotation.Log;
import com.flossom.common.log.enums.BusinessType;
import com.flossom.common.security.annotation.RequiresPermissions;
import com.flossom.system.service.ISysTagService;
import com.flossom.system.service.IWxModeTagService;
import org.apache.commons.compress.utils.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.flossom.system.service.IWxModeService;

/**
 * 模式列表Controller
 *
 * @author flossom
 * @date 2024-01-17
 */
@RestController
@RequestMapping("/mode")
public class WxModeController extends BaseController
{
    @Autowired
    private IWxModeService wxModeService;
    @Autowired
    private IWxModeTagService wxModeTagService;
    @Autowired
    private ISysTagService sysTagService;

    /**
     * 查询模式列表列表
     */
    @RequiresPermissions("system:mode:list")
    @GetMapping("/list")
    public TableDataInfo list(WxMode wxMode)
    {
        startPage();
        List<WxMode> list = wxModeService.selectWxModeList(wxMode);
        // 处理标签显示
        for(WxMode mode : list) {
            List<String> wechatList = Lists.newArrayList();
            List<String> companyList = Lists.newArrayList();
            WxModeTag modeTag = new WxModeTag();
            modeTag.setModeId(mode.getId());
            List<WxModeTag> wxModeTags = wxModeTagService.selectWxModeTagList(modeTag);
            if (CollectionUtils.isNotEmpty(wxModeTags)) {
                for(WxModeTag wxModeTag : wxModeTags) {
                    Long tagId = wxModeTag.getTagId();
                    if (Objects.nonNull(tagId)) {
                        SysTag sysTag = sysTagService.selectDeptById(tagId);
                        if (Objects.nonNull(sysTag)) {
                            if (String.valueOf(TagTypeStatusEnum.MINI_PROGRAM.getCode()).equals(String.valueOf(wxModeTag.getTagType()))) {
                                wechatList.add(sysTag.getTagName());
                            } else {
                                companyList.add(sysTag.getTagName());
                            }
                        }
                    }
                }
            }
            if (CollectionUtils.isNotEmpty(wechatList)) {
                String wechatTag = wechatList.stream().map(String::valueOf).collect(Collectors.joining(","));
                mode.setLockWechatTag(wechatTag);
            }
            if (CollectionUtils.isNotEmpty(companyList)) {
                String companyTag = companyList.stream().map(String::valueOf).collect(Collectors.joining(","));
                mode.setLockCompanyTag(companyTag);
            }
        }
        return getDataTable(list);
    }

    /**
     * 查询模式列列表
     */
    @RequiresPermissions("system:mode:list")
    @GetMapping("/listAll")
    public R listAll(WxMode wxMode)
    {
        return R.ok(wxModeService.selectWxModeList(wxMode));
    }

    /**
     * 导出模式列表列表
     */
    @RequiresPermissions("system:mode:export")
    @Log(title = "模式列表", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, WxMode wxMode)
    {
        List<WxMode> list = wxModeService.selectWxModeList(wxMode);
        ExcelUtil<WxMode> util = new ExcelUtil<WxMode>(WxMode.class);
        util.exportExcel(response, list, "模式列表数据");
    }

    /**
     * 获取模式列表详细信息
     */
    @RequiresPermissions("system:mode:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(wxModeService.selectWxModeById(id));
    }

    /**
     * 新增模式列表
     */
    @RequiresPermissions("system:mode:add")
    @Log(title = "模式列表", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody WxMode wxMode)
    {
        return toAjax(wxModeService.insertWxMode(wxMode));
    }

    /**
     * 修改模式列表
     */
    @RequiresPermissions("system:mode:edit")
    @Log(title = "模式列表", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody WxMode wxMode)
    {
        return toAjax(wxModeService.updateWxMode(wxMode));
    }

    /**
     * 删除模式列表
     */
    @RequiresPermissions("system:mode:remove")
    @Log(title = "模式列表", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(wxModeService.deleteWxModeByIds(ids));
    }
}
