package com.cool.store.controller;

import com.cool.store.context.CurrentUserHolder;
import com.cool.store.context.PartnerUserHolder;
import com.cool.store.dto.calendar.UserCalendarsEventDTO;
import com.cool.store.enums.LineStatusEnum;
import com.cool.store.exception.ApiException;
import com.cool.store.http.ISVHttpRequest;
import com.cool.store.request.*;
import com.cool.store.response.ResponseResult;
import com.cool.store.service.*;
import com.cool.store.vo.*;
import com.cool.store.vo.interview.InterviewVO;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.propertyeditors.CurrencyEditor;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * @Author suzhuhong
 * @Date 2023/5/30 17:19
 * @Version 1.0
 */
@RestController
@Slf4j
public class DeskController {

    @Resource
    DeskService deskService;
    @Resource
    HyPartnerIntentInfoService hyPartnerIntentInfoService;
    @Resource
    HyPartnerInterviewPlanService hyPartnerInterviewPlanService;
    @Resource
    HyPartnerLineInfoService hyPartnerLineInfoService;
    @Resource
    HyPartnerBaseInfoService hyPartnerBaseInfoService;
    @Resource
    EnterpriseUserService enterpriseUserService;
    @Resource
    PartnerUserInfoService partnerUserInfoService;
    @Resource
    HyPhoneLocationService hyPhoneLocationService;



    @GetMapping(path = "/interviewSchedule")
    @ApiOperation("面试日程信息 面试信息有限 不做分页")
    public ResponseResult<InterviewScheduleInfoVO> interviewSchedule(@RequestParam(value = "selectedData",required = false) String selectedData){

        String userId = CurrentUserHolder.getUserId();
        return  ResponseResult.success(deskService.interviewSchedule(userId,selectedData));
    }


    @GetMapping(path = "/getInvestmentManagerList")
    @ApiOperation("招商经理")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "type", value = "allocation-招商经理   transfer", required = false),
            @ApiImplicitParam(name = "keyword", value = "keyword", required = false),
    })
    public  ResponseResult<List<EnterpriseUserSingleInfoVO>>  getInvestmentManagerList(String type,String keyword){
        return ResponseResult.success(enterpriseUserService.getInvestmentManagerList(type,CurrentUserHolder.getUserId(),keyword));
    }


    @GetMapping(path = "/getUserCalendarsEvents")
    @ApiOperation("飞书日程信息")
    public ResponseResult<List<UserCalendarsEventDTO>> getUserCalendarsEvents(@RequestParam(value = "startTime") Long startTime,
                                                                              @RequestParam(value = "endTime") Long endTime) throws ApiException {
        String userId = CurrentUserHolder.getUserId();
        return  ResponseResult.success(deskService.getUserCalendarsEvents(userId,startTime,endTime));
    }



    @GetMapping(path = "/queryStageCount")
    @ApiOperation("招商经理视角====各阶段待处理待跟进数量")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "type", value = "类型  待处理-pending   待跟进-follow", required = false),
    })
    public ResponseResult<StageCountVO> queryStageCount(@RequestParam(value = "type",required = false)String type){
        return ResponseResult.success(deskService.getStageCountByType(CurrentUserHolder.getUserId(),type));
    }

    @GetMapping(path = "/queryInterviewPlan")
    @ApiOperation("是否有面试与临期线索")
    public ResponseResult<HashMap<String, InterviewPlanVO>> queryInterviewPlanVO(){
        String userId = CurrentUserHolder.getUserId();
        return ResponseResult.success(deskService.getInterviewPlan(userId));
    }


    @GetMapping(path = "/queryIntentApplyList")
    @ApiOperation("招商经理视角===意向申请审核列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "type", value = "类型  待处理-pending   待跟进-follow", required = false),
            @ApiImplicitParam(name = "pageNumber", value = "1", required = false),
            @ApiImplicitParam(name = "pageSize", value = "10", required = false),
    })
    public ResponseResult<PageInfo<PartnerIntentApplyInfoVO>> queryIntentApplyList(@RequestParam(value = "type",required = false)String type,
                                                                                   @RequestParam(value = "pageNumber",required = false,defaultValue = "1")Integer pageNumber,
                                                                                   @RequestParam(value = "pageSize",required = false,defaultValue = "10")Integer pageSize){
        String userId = CurrentUserHolder.getUserId();
        return ResponseResult.success(hyPartnerIntentInfoService.getPartnerIntentApplyList(userId,type,pageSize,pageNumber));
    }


    @GetMapping(path = "/querySubscribeInterviewTimeList")
    @ApiOperation("招商经理视角===预约面试时间 列表")
    public ResponseResult<PageInfo<PartnerInterviewInfoVO>> querySubscribeInterviewTimeList(@RequestParam(value = "type",required = false)String type,
                                                                                            @RequestParam(value = "pageNumber",required = false,defaultValue = "1")Integer pageNumber,
                                                                                            @RequestParam(value = "pageSize",required = false,defaultValue = "10")Integer pageSize){

        String userId = CurrentUserHolder.getUserId();
        return ResponseResult.success(hyPartnerInterviewPlanService.getPartnerInterviewInfoList(userId,type,pageSize,pageNumber));
    }

    @GetMapping(path = "/queryQualifiedInterviewList")
    @ApiOperation("招商经理视角===合格资格面试 列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "type", value = "类型  待处理-pending   待跟进-follow", required = false),
    })
    public ResponseResult<PageInfo<PartnerInterviewInfoVO>> queryQualifiedInterviewList(@RequestParam(value = "type",required = false)String type,
                                                                                        @RequestParam(value = "pageNumber",required = false,defaultValue = "1")Integer pageNumber,
                                                                                        @RequestParam(value = "pageSize",required = false,defaultValue = "10")Integer pageSize){
        String userId = CurrentUserHolder.getUserId();
        return ResponseResult.success(hyPartnerInterviewPlanService.getQualifiedInterviewList(userId,type,pageSize,pageNumber));
    }


    @GetMapping(path = "/getPartnerLineInfoAndBaseInfo")
    @ApiOperation("查询加盟商线索详情之一 线索信息与基本信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "lineId", value = "线索ID", required = false),
    })
    public ResponseResult<PartnerLineInfoAndBaseInfoVO> getPartnerLineInfoAndBaseInfo(@RequestParam(value = "lineId",required = false)Long lineId){
        return ResponseResult.success(hyPartnerLineInfoService.selectPartnerLineInfoAndBaseInfo(lineId));
    }


    @GetMapping(path = "/getPartnerIntentInfo")
    @ApiOperation("查询加盟商线索详情 意向信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "lineId", value = "线索ID", required = false),
    })
    public ResponseResult<PartnerIntentInfoVO> getPartnerIntentInfo(@RequestParam(value = "lineId",required = false)Long lineId){
        return ResponseResult.success(hyPartnerIntentInfoService.getPartnerIntentApplyDetail(lineId));
    }


    @GetMapping(path = "/getInviteCodeDetail")
    @ApiOperation("查询加盟商线索详情 邀请码信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "partnerId", value = "partnerId", required = false),
    })
    public ResponseResult<InviteCodeDetailVO> getInviteCodeDetail(@RequestParam(value = "partnerId",required = false)String partnerId){
        return ResponseResult.success(partnerUserInfoService.selectInviteCodeDetail(partnerId));
    }


    @GetMapping(path = "/getFollowHistory")
    @ApiOperation("查询加盟商线索详情 跟进历史")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "partnerId", value = "partnerId", required = false),
    })
    public ResponseResult<List<LineFollowHistoryVO>> getFollowHistory(@RequestParam(value = "partnerId",required = false)String partnerId){
        return ResponseResult.success(hyPartnerLineInfoService.getLineFollowHistory(partnerId));
    }


    @GetMapping(path = "/lastMonthCloseLine")
    @ApiOperation("最近30天结束的线索")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNumber", value = "页码", required = false),
            @ApiImplicitParam(name = "pageSize", value = "分页大小", required = false)
    })
    public ResponseResult<PageInfo<PartnerLineInfoVO>> lastMonthCloseLine(@RequestParam(value = "pageNumber",required = false,defaultValue = "1")Integer pageNumber,
                                                                          @RequestParam(value = "pageSize",required = false,defaultValue = "10")Integer pageSize){

        String userId = CurrentUserHolder.getUserId();
        return ResponseResult.success(hyPartnerLineInfoService.lastMonthCloseLine(userId,pageSize,pageNumber));
    }


    @PostMapping(path = "/allocationInvestmentManager")
    @ApiOperation("分配招商经理/批量分配招商经理")
    public ResponseResult<Boolean> allocationInvestmentManager(@RequestBody AllocationInvestmentManagerRequest request){

        return ResponseResult.success(hyPartnerLineInfoService.allocationInvestmentManager(CurrentUserHolder.getUser(),request.getLineIdList()));
    }


    @PostMapping(path = "/transferInvestmentManager")
    @ApiOperation("转让招商经理")
    public ResponseResult<Boolean> transferInvestmentManager(@RequestBody TransferInvestmentManagerRequest request) throws ApiException {
        return ResponseResult.success(hyPartnerLineInfoService.transferInvestmentManager(CurrentUserHolder.getUser(),request ));
    }


    @PostMapping(path = "/queryPublicSeqLineList")
    @ApiOperation("公海列表")
    public ResponseResult<PageInfo<PublicSeaLineListVo>> queryPublicSeaLineList(@RequestBody LineRequest LineRequest){
        return ResponseResult.success(hyPartnerLineInfoService.publicSeaLineList(CurrentUserHolder.getUserId(),LineRequest));
    }


    @PostMapping(path = "/queryPrivateSeaLineList")
    @ApiOperation("私海列表")
    public ResponseResult<PageInfo<PrivateSeaLineListVo>> queryPrivateSeqLineList(@RequestBody PrivateSeaLineListRequest privateSeaLineListRequest){
        return ResponseResult.success(hyPartnerLineInfoService.privateSeaLineList(CurrentUserHolder.getUserId(),privateSeaLineListRequest));
    }

    @PostMapping(path = "/queryBlackList")
    @ApiOperation("黑名单列表")
    public ResponseResult<PageInfo<BlackListVO>> queryBlackList(@RequestBody LineRequest LineRequest){

        return ResponseResult.success(hyPartnerLineInfoService.getBlackList(LineRequest));
    }


    @PostMapping(path = "/removeBlackList")
    @ApiOperation("移出黑名单")
    public ResponseResult<Boolean> removeBlackList(@RequestBody LineBlackListRequest lineBlackListRequest){

        return ResponseResult.success(hyPartnerLineInfoService.removeBlackList(CurrentUserHolder.getUser(),lineBlackListRequest.getLineId(), LineStatusEnum.PUBLIC_SEAS.getCode(),lineBlackListRequest.getCause()));
    }

    @PostMapping(path = "/joinBlackList")
    @ApiOperation("加入黑名单")
    public ResponseResult<Boolean> joinBlackList(@RequestBody LineBlackListRequest lineBlackListRequest) throws ApiException {

        return ResponseResult.success(hyPartnerLineInfoService.joinBlackList(CurrentUserHolder.getUser(),lineBlackListRequest.getLineId(),LineStatusEnum.BLACKLIST.getCode(),lineBlackListRequest.getCause()));
    }


    @PostMapping(path = "/closeOrPassFollow")
    @ApiOperation("意向申请审核 结束跟进/通过流程")
    public ResponseResult<Boolean> closeOrPassFollow(@RequestBody CloseFollowRequest closeFollowRequest) throws ApiException {
        return ResponseResult.success(hyPartnerLineInfoService.closeOrPassFollow(CurrentUserHolder.getUser(),closeFollowRequest));
    }


    @GetMapping(path = "/queryPartnerBaseInfo")
    @ApiOperation("查看意向申请书——用户基本信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "lineId", value = "线索ID", required = false),
    })
    public ResponseResult<PartnerBaseInfoVO> queryPartnerBaseInfo(@RequestParam(value = "lineId",required = false)Long lineId){
        return ResponseResult.success(hyPartnerBaseInfoService.getByPartnerLineId(lineId));
    }

    @PostMapping(path = "/updatePartnerIntentInfo")
    @ApiOperation("修改加盟商意向信息/行业认知")
    public ResponseResult<Integer> updatePartnerIntentInfo(@RequestBody PartnerIntentInfoRequest partnerIntentInfoRequest){
        return ResponseResult.success(hyPartnerIntentInfoService.submitPartnerIntentInfo(partnerIntentInfoRequest));
    }

    @PostMapping(path = "/changeIntentInfo")
    @ApiOperation("员工端变更C端用户意向信息")
    public ResponseResult<Boolean> changeIntentInfo( @RequestBody BaseUserInfoRequest baseUserInfoRequest){
        return ResponseResult.success(hyPartnerIntentInfoService.updatePartnerIntentInfo(CurrentUserHolder.getUser(),baseUserInfoRequest));
    }


    @PostMapping(path = "/addTags")
    @ApiOperation("添加标签接口")
    public ResponseResult<Boolean> addTags(@RequestBody AddTagsRequest addTagsRequest){
        return ResponseResult.success(hyPartnerBaseInfoService.addTags(CurrentUserHolder.getUser(),addTagsRequest));
    }


    @GetMapping(path = "/getAssignFollowUser")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "partnerId", value = "partnerId", required = false),
    })
    public ResponseResult<String> getFollowHistory(@RequestParam(value = "partnerId",required = false)String partnerId,
                                                                      @RequestParam(value = "type",required = false)String type){
        return ResponseResult.success(hyPartnerLineInfoService.getAssignFollowUser(partnerId,type));
    }

    @GetMapping(path = "/assignFollowUser")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "partnerId", value = "partnerId", required = false),
    })
    public ResponseResult<Boolean> getFollowHistory(@RequestParam(value = "partnerId",required = false)String partnerId,
                                                   @RequestParam(value = "wantShopArea",required = false)String wantShopArea,
                                                   @RequestParam(value = "acceptAdjustType",required = false)Integer acceptAdjustType){
        return ResponseResult.success(hyPartnerLineInfoService.assignFollowUser(partnerId,wantShopArea,acceptAdjustType,Boolean.TRUE));
    }


    @GetMapping(path = "/getInterviewInfoByLineId")
    @ApiOperation("查询加盟商线索详情 根据线索信息查询面试信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "lineId", value = "线索ID", required = false),
    })
    public ResponseResult<InterviewVO> getInterviewInfoByLineId(@RequestParam(value = "lineId",required = false)Long lineId,
                                                                @RequestParam(value = "needDevelopmentDirector",required = false)Boolean needDevelopmentDirector) throws ApiException {
        return ResponseResult.success(hyPartnerLineInfoService.getInterviewInfo(lineId,needDevelopmentDirector));
    }
}
