package com.fs.company.controller.qw;

import com.fs.common.core.controller.BaseController;
import com.fs.common.core.domain.AjaxResult;
import com.fs.common.core.page.TableDataInfo;
import com.fs.common.exception.CustomException;
import com.fs.common.utils.poi.ExcelUtil;
import com.fs.qw.domain.FsCompanyCustomerLog;
import com.fs.qw.service.IFsCompanyCustomerLogService;
import com.github.pagehelper.PageHelper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;

/**
 * 客户信息认领记录Controller
 *
 * @author ruoyi
 * @date 2026-05-28
 */
@Slf4j
@RestController
@RequestMapping("/qw/customerLog")
@Api(tags = "客户认领记录管理")
public class FsCompanyCustomerLogController extends BaseController {

    @Autowired
    private IFsCompanyCustomerLogService fsCompanyCustomerLogService;

    /**
     * 查询客户认领记录列表（分页）
     */
    @ApiOperation("分页查询认领记录")
    @GetMapping("/list")
    public TableDataInfo list(FsCompanyCustomerLog fsCompanyCustomerLog) {
        PageHelper.startPage(fsCompanyCustomerLog.getPageNum(), fsCompanyCustomerLog.getPageSize());
        List<FsCompanyCustomerLog> list = fsCompanyCustomerLogService.selectFsCompanyCustomerLogList(fsCompanyCustomerLog);
        return getDataTable(list);
    }

    /**
     * 导出客户认领记录
     */
    @ApiOperation("导出认领记录")
    @PostMapping("/export")
    public void export(HttpServletResponse response, FsCompanyCustomerLog fsCompanyCustomerLog) {
        List<FsCompanyCustomerLog> list = fsCompanyCustomerLogService.selectFsCompanyCustomerLogList(fsCompanyCustomerLog);
        ExcelUtil<FsCompanyCustomerLog> util = new ExcelUtil<>(FsCompanyCustomerLog.class);
        try {
            util.exportExcel(response, list, "客户认领记录数据");
        } catch (Exception e) {
            log.error("导出客户认领记录数据异常", e);
            throw new CustomException("导出客户认领记录数据异常");
        }
    }

    /**
     * 获取客户认领记录详细信息
     */
    @ApiOperation("获取记录详情")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        FsCompanyCustomerLog fsCompanyCustomerLog = fsCompanyCustomerLogService.selectFsCompanyCustomerLogById(id);
        return AjaxResult.success(fsCompanyCustomerLog);
    }

    /**
     * 新增客户认领记录
     */
    @ApiOperation("新增认领记录")
    @PostMapping
    public AjaxResult add(@RequestBody FsCompanyCustomerLog fsCompanyCustomerLog) {
        // 设置认领时间为当前时间
        fsCompanyCustomerLog.setClaimTime(new Date());
        try {
            return toAjax(fsCompanyCustomerLogService.insertFsCompanyCustomerLog(fsCompanyCustomerLog));
        } catch (DuplicateKeyException e) {
            return AjaxResult.error("数据冲突，请稍后重试");
        }
    }

    /**
     * 修改客户认领记录
     */
    @ApiOperation("修改认领记录")
    @PutMapping
    public AjaxResult edit(@RequestBody FsCompanyCustomerLog fsCompanyCustomerLog) {
        try {
            return toAjax(fsCompanyCustomerLogService.updateFsCompanyCustomerLog(fsCompanyCustomerLog));
        } catch (DuplicateKeyException e) {
            return AjaxResult.error("数据冲突，请稍后重试");
        }
    }

    /**
     * 删除客户认领记录
     */
    @ApiOperation("删除认领记录")
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return AjaxResult.success(fsCompanyCustomerLogService.deleteFsCompanyCustomerLogByIds(ids));
    }
}