package com.flossom.system.controller;

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.WxUserScriptLog;
import com.flossom.common.core.domain.req.AllSendScriptOperateReq;
import com.flossom.common.core.domain.req.WxUserMemberVm;
import com.flossom.common.core.domain.req.WxUserScriptReq;
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.IWxUserScriptLogService;

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

/**
 * 发送话术记录Controller
 *
 * @author flossom
 * @date 2023-12-20
 */
@RestController
@RequestMapping("/userScriptLog")
public class WxUserScriptLogController extends BaseController {

    @Autowired
    private IWxUserScriptLogService wxUserScriptLogService;

    @Autowired
    private IWxUserMemberService wxUserMemberService;

    /**
     * 查询发送话术记录列表
     */
    @RequiresPermissions("system:user_script_log:list")
    @GetMapping("/list")
    public TableDataInfo list(WxUserScriptLog wxUserScriptLog) {
        startPage();
        List<WxUserScriptLog> list = wxUserScriptLogService.selectWxUserScriptLogList(wxUserScriptLog);
        return getDataTable(list);
    }

    /**
     * 导出发送话术记录列表
     */
    @RequiresPermissions("system:user_script_log:export")
    @Log(title = "发送话术记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, WxUserScriptLog wxUserScriptLog) {
        List<WxUserScriptLog> list = wxUserScriptLogService.selectWxUserScriptLogList(wxUserScriptLog);
        ExcelUtil<WxUserScriptLog> util = new ExcelUtil<WxUserScriptLog>(WxUserScriptLog.class);
        util.exportExcel(response, list, "发送话术记录数据");
    }

    /**
     * 获取发送话术记录详细信息
     */
    @RequiresPermissions("system:user_script_log:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(wxUserScriptLogService.selectWxUserScriptLogById(id));
    }

    /**
     * 新增发送话术记录
     */
    @RequiresPermissions("system:user_script_log:add")
    @Log(title = "发送话术记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody WxUserScriptLog wxUserScriptLog) {
        return toAjax(wxUserScriptLogService.insertWxUserScriptLog(wxUserScriptLog));
    }

    /**
     * 修改发送话术记录
     */
    @RequiresPermissions("system:user_script_log:edit")
    @Log(title = "发送话术记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody WxUserScriptLog wxUserScriptLog) {
        return toAjax(wxUserScriptLogService.updateWxUserScriptLog(wxUserScriptLog));
    }

    /**
     * 删除发送话术记录
     */
    @RequiresPermissions("system:user_script_log:remove")
    @Log(title = "发送话术记录", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(wxUserScriptLogService.deleteWxUserScriptLogByIds(ids));
    }

    /**
     * 发送话术
     *
     * @return
     */
    @PostMapping("/batchSendScript")
    public R batchSendScript(@RequestBody @Validated WxUserScriptReq wxUserScriptReq) {
        if (wxUserScriptReq.getUserIdList() == null || wxUserScriptReq.getUserIdList().size() == 0) {
            return R.fail("请选择话术接收人");
        }
        if (wxUserScriptReq.getIsCustom() == 0) {
            // 模板话术
            if (wxUserScriptReq.getScriptTemplateId() == null || wxUserScriptReq.getScriptTemplateId() == 0) {
                return R.fail("请选择话术标题");
            }
        } else if (wxUserScriptReq.getIsCustom() == 1) {
            // 自定义话术
            if (StringUtils.isBlank(wxUserScriptReq.getTitile())) {
                return R.fail("请输入话术标题");
            }
            if (StringUtils.isBlank(wxUserScriptReq.getContent())) {
                return R.fail("请输入话术内容");
            }
            // 跳转类型
            if (wxUserScriptReq.getType() == 1) {
                // 1跳转内部链接
                if (StringUtils.isBlank(wxUserScriptReq.getLink())) {
                    return R.fail("请输入内部链接");
                }
                if (StringUtils.isBlank(wxUserScriptReq.getLinkParams())) {
                    return R.fail("请输入跳转参数");
                }
            }
            if (wxUserScriptReq.getType() == 3) {
                // 03跳转外部链接
                if (StringUtils.isBlank(wxUserScriptReq.getLink())) {
                    return R.fail("请输入外部链接地址");
                }
            }
            if (wxUserScriptReq.getType() == 4) {
                // 4跳转小程序
                if (StringUtils.isBlank(wxUserScriptReq.getRedirectAppid())) {
                    return R.fail("请输入小程序appid");
                }
                if (StringUtils.isBlank(wxUserScriptReq.getRedirectUrl())) {
                    return R.fail("请输入小程序页面地址");
                }
            }
            if (wxUserScriptReq.getType() == 5) {
                // 5导向视频号
                if (StringUtils.isBlank(wxUserScriptReq.getVideoNo())) {
                    return R.fail("请输入视频号id");
                }
                if(!wxUserScriptReq.getVideoNo().startsWith("sph")){
                    return R.fail("请输入以“sph”开头的视频号id，可在视频号助手获取");
                }
                if (StringUtils.isBlank(wxUserScriptReq.getFeedId())) {
                    return R.fail("请输入视频号feedid");
                }
            }
            if (wxUserScriptReq.getType() == 6) {
                // 6导向视频号直播间
                if (StringUtils.isBlank(wxUserScriptReq.getVideoNo())) {
                    return R.fail("请输入视频号id");
                }
                if(!wxUserScriptReq.getVideoNo().startsWith("sph")){
                    return R.fail("请输入以“sph”开头的视频号id，可在视频号助手获取");
                }
            }
        }
        wxUserScriptLogService.sendScript(wxUserScriptReq);
        return R.ok();
    }

    /**
     * 发送话术
     *
     * @return
     */
    @PostMapping("/allSendScript")
    public R allSendScript(@RequestBody @Validated AllSendScriptOperateReq allSendScriptOperateReq) {
        WxUserScriptReq wxUserScriptReq = allSendScriptOperateReq.getWxUserScriptReq();
        WxUserMemberVm wxUserMemberVm = allSendScriptOperateReq.getWxUserMemberVm();

        if (wxUserScriptReq.getIsCustom() == 0) {
            // 模板话术
            if (wxUserScriptReq.getScriptTemplateId() == null || wxUserScriptReq.getScriptTemplateId() == 0) {
                return R.fail("请选择话术标题");
            }
        } else if (wxUserScriptReq.getIsCustom() == 1) {
            // 自定义话术
            if (StringUtils.isBlank(wxUserScriptReq.getTitile())) {
                return R.fail("请输入话术标题");
            }
            if (StringUtils.isBlank(wxUserScriptReq.getContent())) {
                return R.fail("请输入话术内容");
            }
            // 跳转类型
            if (wxUserScriptReq.getType() == 1) {
                // 1跳转内部链接
                if (StringUtils.isBlank(wxUserScriptReq.getLink())) {
                    return R.fail("请输入内部链接");
                }
                if (StringUtils.isBlank(wxUserScriptReq.getLinkParams())) {
                    return R.fail("请输入跳转参数");
                }
            }
            if (wxUserScriptReq.getType() == 3) {
                // 03跳转外部链接
                if (StringUtils.isBlank(wxUserScriptReq.getLink())) {
                    return R.fail("请输入外部链接地址");
                }
            }
            if (wxUserScriptReq.getType() == 4) {
                // 4跳转小程序
                if (StringUtils.isBlank(wxUserScriptReq.getRedirectAppid())) {
                    return R.fail("请输入小程序appid");
                }
                if (StringUtils.isBlank(wxUserScriptReq.getRedirectUrl())) {
                    return R.fail("请输入小程序页面地址");
                }
            }
            if (wxUserScriptReq.getType() == 5) {
                // 5导向视频号
                if (StringUtils.isBlank(wxUserScriptReq.getVideoNo())) {
                    return R.fail("请输入视频号id");
                }
                if(!wxUserScriptReq.getVideoNo().startsWith("sph")){
                    return R.fail("请输入以“sph”开头的视频号id，可在视频号助手获取");
                }
                if (StringUtils.isBlank(wxUserScriptReq.getFeedId())) {
                    return R.fail("请输入视频号feedid");
                }
            }
            if (wxUserScriptReq.getType() == 6) {
                // 6导向视频号直播间
                if (StringUtils.isBlank(wxUserScriptReq.getVideoNo())) {
                    return R.fail("请输入视频号id");
                }
                if(!wxUserScriptReq.getVideoNo().startsWith("sph")){
                    return R.fail("请输入以“sph”开头的视频号id，可在视频号助手获取");
                }
            }
        }

        List<WxUserMember> list = wxUserMemberService.selectWxUserMemberList(wxUserMemberVm);
        if (list != null && list.size() > 0) {
            List<Long> collect = list.stream().map(wxUserMember -> wxUserMember.getId()).collect(Collectors.toList());
            wxUserScriptReq.setUserIdList(collect);
            wxUserScriptLogService.sendScript(wxUserScriptReq);
        }
        return R.ok();
    }

}
