package com.flossom.system.controller;

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

import com.flossom.common.core.domain.R;
import com.flossom.common.core.domain.entity.WxUserMember;
import com.flossom.common.core.domain.entity.WxUserRemark;
import com.flossom.common.core.domain.req.WxUserIntegralVm;
import com.flossom.common.core.domain.req.WxUserMemberVm;
import com.flossom.common.core.domain.req.WxUserRemarkReq;
import com.flossom.common.core.enums.IntegralChangeTypeEnum;
import com.flossom.common.core.exception.ServiceException;
import com.flossom.common.core.utils.StringUtils;
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.IWxUserMemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import com.flossom.system.service.IWxUserRemarkService;

/**
 * 微信用户备注列Controller
 *
 * @author flossom
 * @date 2023-12-21
 */
@RestController
@RequestMapping("/wxUserRemark")
public class WxUserRemarkController extends BaseController {
    @Autowired
    private IWxUserRemarkService wxUserRemarkService;

    @Autowired
    private IWxUserMemberService wxUserMemberService;

    /**
     * 查询微信用户备注列列表
     */
    @RequiresPermissions("system:wxUserRemark:list")
    @GetMapping("/list")
    public TableDataInfo list(WxUserRemark wxUserRemark) {
        startPage();
        List<WxUserRemark> list = wxUserRemarkService.selectWxUserRemarkList(wxUserRemark);
        return getDataTable(list);
    }

    /**
     * 导出微信用户备注列列表
     */
    @RequiresPermissions("system:wxUserRemark:export")
    @Log(title = "微信用户备注列", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, WxUserRemark wxUserRemark) {
        List<WxUserRemark> list = wxUserRemarkService.selectWxUserRemarkList(wxUserRemark);
        ExcelUtil<WxUserRemark> util = new ExcelUtil<WxUserRemark>(WxUserRemark.class);
        util.exportExcel(response, list, "微信用户备注列数据");
    }

    /**
     * 获取微信用户备注列详细信息
     */
    @RequiresPermissions("system:wxUserRemark:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(wxUserRemarkService.selectWxUserRemarkById(id));
    }

    /**
     * 新增微信用户备注列
     */
    @RequiresPermissions("system:wxUserRemark:add")
    @Log(title = "微信用户备注列", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody WxUserRemark wxUserRemark) {
        return toAjax(wxUserRemarkService.insertWxUserRemark(wxUserRemark));
    }

    /**
     * 修改微信用户备注列
     */
    @RequiresPermissions("system:wxUserRemark:edit")
    @Log(title = "微信用户备注列", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody WxUserRemark wxUserRemark) {
        return toAjax(wxUserRemarkService.updateWxUserRemark(wxUserRemark));
    }

    /**
     * 删除微信用户备注列
     */
    @RequiresPermissions("system:wxUserRemark:remove")
    @Log(title = "微信用户备注列", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(wxUserRemarkService.deleteWxUserRemarkByIds(ids));
    }

    /**
     * 批量 添加备注
     */
    @PostMapping("/batchAddRemark")
    public R batchAddRemark(@RequestBody @Validated WxUserRemarkReq wxUserRemarkReq) {
        if(wxUserRemarkReq.getUserIdList() == null || wxUserRemarkReq.getUserIdList().size() == 0) {
            return R.fail("请选择用户");
        }
        wxUserRemarkService.batchAddRemark(wxUserRemarkReq);
        return R.ok();
    }

    /**
     * 全量 添加备注
     */
    @PostMapping("/allAddRemark")
    public R allAddRemark(@RequestParam("content") String content, @RequestBody WxUserMemberVm wxUserMemberVm) {
        if(StringUtils.isBlank(content)) {
            throw new ServiceException("请输入备注内容");
        }
        WxUserRemarkReq wxUserRemarkReq = new WxUserRemarkReq();
        wxUserRemarkReq.setContent(content);
        List<WxUserMember> list = wxUserMemberService.selectWxUserMemberList(wxUserMemberVm);
        if (list != null && list.size() > 0) {
            List<Long> collect = list.stream().map(wxUserMember -> wxUserMember.getId()).collect(Collectors.toList());
            wxUserRemarkReq.setUserIdList(collect);
            wxUserRemarkService.batchAddRemark(wxUserRemarkReq);
        }
        return R.ok();
    }
}
