package com.ruoyi.demo.controller;

import cn.dev33.satoken.stp.StpUtil;

import com.ruoyi.common.core.domain.R;
import com.ruoyi.demo.entity.bo.WorkTableBo;
import com.ruoyi.demo.entity.vo.AddFollowRecordVo;
import com.ruoyi.demo.entity.vo.TurnBusinessOpportunitiesVo;
import com.ruoyi.demo.entity.vo.WorkTableSearchVo;
import com.ruoyi.demo.service.AttentionPoolService;
import com.ruoyi.demo.service.BusinessOpportunitiesService;
import com.ruoyi.demo.service.DatabackService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotBlank;

@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/work")
public class WorkTableController {
    @Autowired
    DatabackService databackService;
    @Autowired
    AttentionPoolService attentionPoolService;
    @Autowired
    BusinessOpportunitiesService businessOpportunitiesService;

    /**
     * 获取数据概览
     *
     * @param bo 开始时间和截止时间查询请求体
     * @return
     */
    @PostMapping("/databack/about")
    public R about(@RequestBody WorkTableBo bo) {
        // 获取用户id
        String loginId = String.valueOf(StpUtil.getLoginId());
        Long userid = Long.valueOf(loginId.split(":")[1]);
        String userId = userid.toString();
        if (userId == null || userId.equals(""))
            return R.fail("请登录");
        return R.ok(databackService.searchAbout(userId, bo));
    }

    /**
     * 获取客户数量变化趋势
     *
     * @param condition 条件
     * @return
     */
    @GetMapping("/databack/trend")
    public R trend(String condition) {
        // 获取用户id
        String loginId = String.valueOf(StpUtil.getLoginId());
        Long userid = Long.valueOf(loginId.split(":")[1]);
        String userId = userid.toString();
        if (userId == null || userId.equals(""))
            return R.fail("请登录");
        return R.ok(databackService.searchTrand(userId, condition));
    }

    /**
     * 分页获取潜客列表
     *
     * @param bo 请求体
     * @return
     */
    @PostMapping("/pool/list")
    public R poolList(WorkTableSearchVo bo) {
        // 获取用户id
        String loginId = String.valueOf(StpUtil.getLoginId());
        Long userid = Long.valueOf(loginId.split(":")[1]);
        String userId = userid.toString();
        if (userId == null || userId.equals(""))
            return R.fail("请登录");
        return R.ok(attentionPoolService.searchList(bo, userId));
    }

    /**
     * 取消关注
     *
     * @param attentionId 潜客id
     * @return
     */
    @DeleteMapping("/pool")
    public R cancel(@NotBlank String attentionId) {
        // 获取用户id
        String loginId = String.valueOf(StpUtil.getLoginId());
        Long userid = Long.valueOf(loginId.split(":")[1]);
        String userId = userid.toString();
        if (userId == null || userId.equals(""))
            return R.fail("请登录");
        int flag = attentionPoolService.cancelAttention(attentionId, userId);
        if (flag == 1)
            return R.ok("取消关注成功！");
        return R.fail("取消关注失败。");
    }

    /**
     * 转化商机
     *
     * @param turnVo
     * @return
     */
    @PostMapping("/pool")
    public R turn(TurnBusinessOpportunitiesVo turnVo) {
        // 获取用户id
        String loginId = String.valueOf(StpUtil.getLoginId());
        Long userid = Long.valueOf(loginId.split(":")[1]);
        String userId = userid.toString();
        if (userId == null || userId.equals(""))
            return R.fail("请登录");
        int flag = attentionPoolService.turnBusinessOpportunities(turnVo, userId);
        if (flag == 1)
            return R.ok("转化成功！");
        if (flag == 2)
            return R.ok("已转化。");
        return R.fail("转化失败。");
    }

    /**
     * 分页获取线索列表
     *
     * @param vo
     * @return
     */
    @PostMapping("/clue/list")
    public R clueList(WorkTableSearchVo vo) {
        // 获取用户id
        String loginId = String.valueOf(StpUtil.getLoginId());
        Long userid = Long.valueOf(loginId.split(":")[1]);
        String userId = userid.toString();
        if (userId == null || userId.equals(""))
            return R.fail("请登录");
        return R.ok(businessOpportunitiesService.searchList(vo, userId));
    }

    /**
     * 分页获取跟进记录
     *
     * @param vo
     * @return
     */
    @PostMapping("/follow/list")
    public R followList(WorkTableSearchVo vo) {
        // 获取用户id
        String loginId = String.valueOf(StpUtil.getLoginId());
        Long userid = Long.valueOf(loginId.split(":")[1]);
        String userId = userid.toString();
        if (userId == null || userId.equals(""))
            return R.fail("请登录");
        return R.ok(businessOpportunitiesService.searchFollowList(vo, userId));
    }

    /**
     * 添加跟进记录
     *
     * @param addVo
     * @return
     */
    @PostMapping("/follow")
    public R followAdd(AddFollowRecordVo addVo) {
        // 获取用户id
        String loginId = String.valueOf(StpUtil.getLoginId());
        Long userid = Long.valueOf(loginId.split(":")[1]);
        String userId = userid.toString();
        if (userId == null || userId.equals(""))
            return R.fail("请登录");
        int flag = businessOpportunitiesService.addFollow(addVo, userId);
        if (flag == 1)
            return R.ok("添加成功！");
        return R.fail("添加失败");
    }

    /**
     * 品牌关注
     *
     * @param brandId 品牌id
     * @return
     */
    @PostMapping("/brand")
    public R brandAttention(@RequestParam("brandId") String brandId) {
        String loginId = String.valueOf(StpUtil.getLoginId());
        Long userid = Long.valueOf(loginId.split(":")[1]);
        String userId = userid.toString();
        if (userId == null || userId.equals(""))
            return R.fail("请登录");
        int flag = attentionPoolService.attentionBrand(brandId, userId);
        if (flag == 1) {
            return R.ok("关注成功！");
        } else if (flag == 2) {
            return R.fail("重复关注。");
        } else if (flag == 3) {
            return R.fail("未知品牌");
        }
        return R.fail("关注失败。");
    }

    /**
     * 品牌取消关注
     *
     * @param brandId 品牌id
     * @return
     */
    @DeleteMapping("/brand")
    public R brandCancel(String brandId) {
        String loginId = String.valueOf(StpUtil.getLoginId());
        Long userid = Long.valueOf(loginId.split(":")[1]);
        String userId = userid.toString();
        if (userId == null || userId.equals(""))
            return R.fail("请登录");
        int flag = attentionPoolService.cancelBrand(brandId, userId);
        if (flag == 1) {
            return R.ok("取消关注成功！");
        }
        return R.fail("取消关注失败。");
    }

    /**
     * 网点关注
     *
     * @param wdId 网点id
     * @return
     */
    @PostMapping("/wdInfo")
    public R wdInfoAttention(@RequestParam("wdId") String wdId) {
        String loginId = String.valueOf(StpUtil.getLoginId());
        Long userid = Long.valueOf(loginId.split(":")[1]);
        String userId = userid.toString();
        if (userId == null || userId.equals(""))
            return R.fail("请登录");
        int flag = attentionPoolService.attentionWdInfo(wdId, userId);
        if (flag == 1) {
            return R.ok("关注成功！");
        } else if (flag == 2) {
            return R.fail("重复关注。");
        } else if (flag == 3) {
            return R.fail("未知网点");
        }
        return R.fail("关注失败。");
    }

    /**
     * 网点取消关注
     *
     * @param wdId 网点id
     * @return
     */
    @DeleteMapping("/wdInfo")
    public R wdInfoCancel(@RequestParam("wdId") String wdId) {
        String loginId = String.valueOf(StpUtil.getLoginId());
        Long userid = Long.valueOf(loginId.split(":")[1]);
        String userId = userid.toString();
        if (userId == null || userId.equals(""))
            return R.fail("请登录");
        int flag = attentionPoolService.cancelWdInfo(wdId, userId);
        if (flag == 1) {
            return R.ok("取消关注成功！");
        }
        return R.fail("取消关注失败。");
    }
}
