package com.fs.users.controller;

import com.alibaba.fastjson.JSON;
import com.fs.common.core.domain.R;
import com.fs.common.core.page.TableDataInfo;
import com.fs.common.utils.ServletUtils;
import com.fs.core.security.LoginUser;
import com.fs.core.web.service.TokenService;
import com.fs.qw.domain.CustomerTransferApproval;
import com.fs.qw.service.ICustomerTransferApprovalService;
import com.fs.store.dto.FsUserTransferParamDTO;
import com.fs.store.param.SelectCusListPageParam;
import com.fs.store.service.IFsUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * 客户转移审批Controller
 *
 * @author fs
 * @date 2025-04-01
 */
@Slf4j
@RestController
@RequestMapping("/fsuser/user")
public class MyCompanyCustomerController {

    @Autowired
    private ICustomerTransferApprovalService transferApprovalService;

    @Autowired
    private IFsUserService fsUserService;

    @Autowired
    private TokenService tokenService;


    /**
     * 列出当前公司的客户
     * @param param 参数
     * @return 返回数据
     */
    @GetMapping("/list")
    public TableDataInfo list(SelectCusListPageParam param){
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        Long companyId = loginUser.getCompany().getCompanyId();
        param.setCompanyId(companyId);
        return fsUserService.selectCusListPage(param);
    }
    /**
     * 导出
     */
    @PostMapping("/export")
    public void export(){

    }
    /**
     * 转移
     * @param param
     */
    @PostMapping("/transfer")
    public R transfer(@RequestBody FsUserTransferParamDTO param){
        log.info("客户转移: {}",param);
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        param.setSourceCompanyUserId(loginUser.getUser().getUserId());
        CustomerTransferApproval customerTransferApproval = new CustomerTransferApproval();
        customerTransferApproval.setCorpId(String.valueOf(loginUser.getCompany().getCompanyId()));
        customerTransferApproval.setCustomerIds(JSON.toJSONString(param.getUserIds()));
        customerTransferApproval.setOriginalUserId(param.getSourceCompanyUserId());
        customerTransferApproval.setTargetUserId(param.getTargetCompanyUserId());
        customerTransferApproval.setInitiatorUserId(param.getSourceCompanyUserId());
        customerTransferApproval.setContent(param.getContent());
        customerTransferApproval.setCreatedAt(new Date());
        customerTransferApproval.setUpdatedAt(new Date());
        customerTransferApproval.setApprovalStatus(0);
        customerTransferApproval.setTransferType(2);
        transferApprovalService.insertCustomerTransferApproval(customerTransferApproval);
        return R.ok("转移申请已经提交，等待总后台审核!");
    }

}
