package com.pusong.business.controller;

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

import cn.dev33.satoken.annotation.SaIgnore;
import com.pusong.business.domain.bo.PsCustomCallbackBo;
import com.pusong.business.domain.bo.PsCustomPriceBo;
import com.pusong.business.domain.bo.PsCustomRecordQueryBo;
import com.pusong.business.domain.vo.PsCustomerRecordVo;
import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.pusong.common.idempotent.annotation.RepeatSubmit;
import com.pusong.common.log.annotation.Log;
import com.pusong.common.web.core.BaseController;
import com.pusong.common.mybatis.core.page.PageQuery;
import com.pusong.common.core.domain.R;
import com.pusong.common.core.validate.AddGroup;
import com.pusong.common.core.validate.EditGroup;
import com.pusong.common.log.enums.BusinessType;
import com.pusong.common.excel.utils.ExcelUtil;
import com.pusong.business.domain.vo.PsCustomInfoVo;
import com.pusong.business.domain.bo.PsCustomInfoBo;
import com.pusong.business.service.IPsCustomInfoService;
import com.pusong.common.mybatis.core.page.TableDataInfo;

/**
 * 客户基本信息
 *
 * @author Lion Li
 * @date 2024-07-25
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/business/customInfo")
@SaIgnore
public class PsCustomController extends BaseController {

    private final IPsCustomInfoService psCustomInfoService;
    /**
     * 查询登陆人下所有的客户名称和id
     */

    @SaCheckPermission("business:customInfo:list")
    @GetMapping("/listByLoginUser")
    public R<List<Map<String,Object>>> listByLoginUser(String name) {
        return R.ok(psCustomInfoService.listByLoginUser(name));
    }
    /**
     * 查询客户信息列表
     */
    @SaCheckPermission("business:customInfo:list")
    @GetMapping("/list")
    public TableDataInfo<PsCustomInfoVo> list(PsCustomInfoBo bo, PageQuery pageQuery) {
        return psCustomInfoService.queryPageList(bo, pageQuery);
    }

//    /**
//     * 导出客户基本信息列表
//     */
//    @SaCheckPermission("business:customInfo:export")
//    @Log(title = "客户基本信息", businessType = BusinessType.EXPORT)
//    @PostMapping("/export")
//    public void export(PsCustomInfoBo bo, HttpServletResponse response) {
//        //todo 导出接口
//        PageQuery pageQuery = new PageQuery();
//        pageQuery.setPageNum(1);
//        pageQuery.setPageSize(100000);
//        TableDataInfo<PsCustomInfoVo> list = psCustomInfoService.queryPageList(bo,pageQuery);
//        ExcelUtil.exportExcel(list, "客户基本信息", PsCustomInfoVo.class, response);
//    }

    /**
     * 新增客户基本信息
     */
    @SaCheckPermission("business:customInfo:add")
    @Log(title = "新增客户基本信息", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Long> add(@Validated(AddGroup.class) @RequestBody PsCustomInfoBo bo) {
        return R.ok(psCustomInfoService.insertByBo(bo));
    }

    /**
     * 修改客户基本信息
     */
    @SaCheckPermission("business:customInfo:edit")
    @Log(title = "客户基本信息", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody PsCustomInfoBo bo) {
        return toAjax(psCustomInfoService.updateByBo(bo));
    }

    /**
     * 新增客户报价金额
     */
    @SaCheckPermission("business:customInfo:edit")
    @Log(title = "新增客户报价金额", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping("/insertPrice")
    public R<Void> insertPrice(@Validated(AddGroup.class) @RequestBody List<PsCustomPriceBo> bos) {
        return toAjax(psCustomInfoService.insertPiceByBo(bos));
    }
    /**
     * 新增客户回访记录
     */
    @SaCheckPermission("business:customInfo:edit")
    @Log(title = "新增客户回访记录", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping("/insertCallback")
    public R<Void> insertCallback(@Validated(AddGroup.class) @RequestBody List<PsCustomCallbackBo> bos) {
        return toAjax(psCustomInfoService.insertCallbackByBo(bos));
    }

    /**
     * 状态变更
     *
     * @param id 主键串
     * @param type 操作类型  1移入公海 2拉黑
     */
    @RepeatSubmit()
    @SaCheckPermission("business:customInfo:edit")
    @Log(title = "客户状态变更操作", businessType = BusinessType.UPDATE)
    @GetMapping("/updateStatus")
    public R<Void> updateStatus(@NotNull(message = "主键不能为空")  Long id,
                                @NotNull(message = "操作类型不能为空")  Integer type,
                                 String desc) {
        return toAjax(psCustomInfoService.updateCustomerStatus(id, type,desc));
    }


    /**
     * 已完成客户
     * @param queryBo 查询
     * @return
     */
    @SaCheckPermission("business:customInfo:record")
    @Log(title = "已完成客户" )
    @GetMapping("/record")
    public TableDataInfo<PsCustomerRecordVo> record(PsCustomRecordQueryBo queryBo, PageQuery pageQuery) {
        return psCustomInfoService.getCustomerRecordList(queryBo,pageQuery);
    }


    /**
     * 移出黑名单
     * @param customerId 客户id
     * @return
     */
    @SaCheckPermission("business:customInfo:removeblack")
    @RepeatSubmit
    @Log(title = "移除黑名单" )
    @GetMapping("/removeblack")
    public R<Void> removeblack(@NotNull Long customerId) {
         psCustomInfoService.removeblack(customerId);
        return R.ok();
    }
    /**
     * 彻底删除
     * @param customerId 客户id
     * @return
     */
    @SaCheckPermission("business:customInfo:delete")
    @RepeatSubmit
    @Log(title = "彻底删除", businessType=BusinessType.DELETE)
    @GetMapping("/delete")
    public R<Void> delete(@NotNull Long customerId) {
        psCustomInfoService.delete(customerId);
        return R.ok();
    }
    /**
     * 中止合作
     * @param customerId 客户id
     * @return
     */
    @SaCheckPermission("business:customInfo:stop")
    @RepeatSubmit
    @Log(title = "中止合作", businessType=BusinessType.DELETE)
    @GetMapping("/stop")
    public R<Void> stop(@NotNull Long customerId) {
        psCustomInfoService.stop(customerId);
        return R.ok();
    }

    /**
     * 主动认领
     * @param customerId 客户id
     * @return
     */
    @SaCheckPermission("business:customInfo:list")
    @RepeatSubmit
    @Log(title = "主动认领")
    @GetMapping("/claim")
    public R<Void> claim(@NotNull Long customerId) {
        psCustomInfoService.claim(customerId);
        return R.ok();
    }

    /**
     * 指派
     * @param customerId 客户id
     * @param userId 被指派者id
     * @return
     */
    @SaCheckPermission("business:customInfo:list")
    @RepeatSubmit
    @Log(title = "指派")
    @GetMapping("/assign")
    public R<Void> assign(@NotNull Long customerId,@NotNull Long userId) {
        psCustomInfoService.assign(customerId,userId);
        return R.ok();
    }


    /**
     * 导出客户基本信息列表
     */
//    @SaCheckPermission("business:customInfo:export")
//    @Log(title = "客户基本信息", businessType = BusinessType.EXPORT)
//    @PostMapping("/export")
//    public void export(PsCustomInfoBo bo, HttpServletResponse response) {
//        //todo 导出接口
//        List<PsCustomInfoVo> list = psCustomInfoService.queryList(bo);
//        ExcelUtil.exportExcel(list, "客户基本信息", PsCustomInfoVo.class, response);
//    }

}
