package cn.superdesk.uniorder.controller.operationCenter.business;

import cn.superdesk.uniorder.common.rest.response.R;
import cn.superdesk.uniorder.common.security.JwtUtil;
import cn.superdesk.uniorder.common.security.dto.LoginSysUser;
import cn.superdesk.uniorder.controller.base.BaseController;
import cn.superdesk.uniorder.dto.param.operationCenter.business.BusinessDeleteParam;
import cn.superdesk.uniorder.dto.param.operationCenter.business.BusinessQueryParam;
import cn.superdesk.uniorder.dto.param.operationCenter.business.BusinessSaveParam;
import cn.superdesk.uniorder.dto.vo.operationCenter.base.OperationCenterVo;
import cn.superdesk.uniorder.dto.vo.operationCenter.business.BusinessCustomerVo;
import cn.superdesk.uniorder.dto.vo.operationCenter.business.BusinessVo;
import cn.superdesk.uniorder.dto.vo.sys.SysSelectVo;
import cn.superdesk.uniorder.dubbo.consumer.operationCenter.business.OperationCenterBusinessDubboService;
import com.alibaba.fastjson2.JSON;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api(value = "运营中心-业务人员", tags = {"数据管理-业务人员"})
@Slf4j
@RestController
@RequestMapping("/operationCenter/business")
@RequiredArgsConstructor
public class OperationCenterBusinessController extends BaseController {
    private final OperationCenterBusinessDubboService operationCenterBusinessDubboService;

    /**
     * 列表
     */
    @PostMapping("/list")
    public R<List<BusinessVo>> list(@RequestBody @Validated BusinessQueryParam param) {
        log.info("运营中心--业务人员-列表：入参：param:{}", JSON.toJSONString(param));
        return operationCenterBusinessDubboService.getListByParams(param);
    }

    /**
     * 客户管理
     */
    @PostMapping("/getByBusinessId")
    public R<List<BusinessCustomerVo>> getByBusinessId(@RequestBody @Validated BusinessQueryParam param) {
        log.info("运营中心--业务人员-客户管理：入参：param:{}", JSON.toJSONString(param));
        return operationCenterBusinessDubboService.getByBusinessId(param);
    }


    /**
     * 新增/修改
     */
    @PostMapping("/addOrUpdate")
    public R<String> addOrUpdate(@Validated @RequestBody BusinessSaveParam param) {
        log.info("运营中心--业务人员-新增或修改 ：入参：param:{}", JSON.toJSONString(param));
        LoginSysUser loginSysUser = JwtUtil.getLoginSysUser(getToken());
        if(null == loginSysUser || null == loginSysUser.getSysUserId()){
            return  new R("600", "token失效",false );
        }
        param.setOperator(loginSysUser.getFullName());
        param.setOperatorId(loginSysUser.getSysUserId());
        return operationCenterBusinessDubboService.saveBusiness(param);
    }


    /**
     * 根据id查询
     */
    @PostMapping("/get/{id}")
    public R<BusinessVo> get(@PathVariable("id") Long id) {
        log.info("运营中心--业务人员-根据id查询客户信息--->入参：id:{}", id);
        return operationCenterBusinessDubboService.getById(id);
    }

    /**
     * 删除
     */
    @PostMapping("/delete/{id}")
    public R<String> delete(@PathVariable("id") Long id) {
        log.info("运营中心--业务人员-根据id软删除--->入参：id:{}", id);
        LoginSysUser loginSysUser = JwtUtil.getLoginSysUser(getToken());
        if(null == loginSysUser || null == loginSysUser.getSysUserId()){
            return  new R("600", "token失效",false );
        }
        BusinessDeleteParam param = new BusinessDeleteParam();
        param.setOperator(loginSysUser.getFullName());
        param.setOperatorId(loginSysUser.getSysUserId());
        param.setId(id);
        return operationCenterBusinessDubboService.delete(param);
    }


    /**
     * 业务人员下拉列表-新增客户时调用
     */
    @PostMapping("/select")
    public R<List<OperationCenterVo>> select() {
        return operationCenterBusinessDubboService.selectAll();
    }

    @ApiOperation(value = "查询企业下拉列表", notes = "")
    @PostMapping("/selectList")
    @ResponseBody
    public R<List<SysSelectVo>> selectList() {
        R<List<SysSelectVo>> result = new R<>();
        Map<String,Object> params = new HashMap<>();
        result.setData(operationCenterBusinessDubboService.selectList(params));
        return result;
    }
}
