package com.ruoyi.benyun.controller;

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

import java.util.List;

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

//    获取数据概览
    @GetMapping("/databack/about")
    public R about(){
        // 获取用户id
        String userId = "1";
        if (userId == null || userId.equals(""))
            return R.fail("请登录");
        AttentionPoolStatisticsBo bo = databackService.searchAbout(userId);
        return R.ok(bo);
    }

//    获取客户数量变化趋势
    @GetMapping("/databack/trend")
    public R trend(){
        // 获取用户id
        String userId = "1";
        if (userId == null || userId.equals(""))
            return R.fail("请登录");
        List<AttentionPoolStatisticsBo> bos = databackService.searchTrand(userId);
        return R.ok(bos);
    }

//    分页获取潜客列表
    @PostMapping("/pool/list")
    public R poolList(WorkTableSearchVo workTableSearchVo){
        // 获取用户id
        String userId = "1";
        if (userId == null || userId.equals(""))
            return R.fail("请登录");
        PageInfo<List<AttentionPoolBo>> listPageInfo = attentionPoolService.searchList(workTableSearchVo, userId);
        return R.ok(listPageInfo);
    }

//    取消关注
    @DeleteMapping("/pool")
    public R cancel(String attentionId){
        // 获取用户id
        String userId = "1";
        if (userId == null || userId.equals(""))
            return R.fail("请登录");
        if (attentionId == null || attentionId.equals(""))
            return R.fail("attentionId不能为空");
        int flag = attentionPoolService.cancelAttention(attentionId, userId);
        if (flag == 1)
            return R.ok("取消关注成功！");
        return R.fail("取消关注失败。");
    }

//    转化商机
    @PostMapping("/pool")
    public R turn(TurnBusinessOpportunitiesVo turnVo){
        // 获取用户id
        String userId = "1";
        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("转化失败。");
    }

//    分页获取线索列表
    @PostMapping("/clue/list")
    public R clueList(WorkTableSearchVo vo){
        // 获取用户id
        String userId = "1";
        if (userId == null || userId.equals(""))
            return R.fail("请登录");
        return R.ok(businessOpportunitiesService.searchList(vo,userId));
    }

//    分页获取跟进记录
    @PostMapping("/follow/list")
    public R followList(WorkTableSearchVo vo){
        // 获取用户id
        String userId = "1";
        if (userId == null || userId.equals(""))
            return R.fail("请登录");
        return R.ok(businessOpportunitiesService.searchFollowList(vo,userId));
    }

//    添加跟进记录
    @PostMapping("/follow")
    public R followAdd(AddFollowRecordVo addVo){
        // 获取用户id
        String userId = "1";
        if (userId == null || userId.equals(""))
            return R.fail("请登录");
        int flag = businessOpportunitiesService.addFollow(addVo, userId);
        if (flag == 1)
            return R.ok("添加成功！");
        return R.fail("添加失败");
    }

    //    品牌关注
    @PostMapping("/brand")
    public R brandAttention(String brandId){
        String userId = "1";
        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("关注失败。");
    }

    //    品牌取消关注
    @DeleteMapping("/brand")
    public R brandCancel(String brandId){
        String userId = "1";
        if (userId == null || userId.equals(""))
            return R.fail("请登录");
        int flag = attentionPoolService.cancelBrand(brandId, userId);
        if (flag == 1){
            return R.ok("取消关注成功！");
        }
        return R.fail("取消关注失败。");
    }

//    网点关注
    @PostMapping("/wdInfo")
    public R wdInfoAttention(String wdId){
        String userId = "1";
        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("关注失败。");
    }

//    网点取消关注
    @DeleteMapping("/wdInfo")
    public R wdInfoCancel(String wdId){
        String userId = "1";
        if (userId == null || userId.equals(""))
            return R.fail("请登录");
        int flag = attentionPoolService.cancelWdInfo(wdId, userId);
        if (flag == 1){
            return R.ok("取消关注成功！");
        }
        return R.fail("取消关注失败。");
    }
}
