package com.ruoyi.credit.controller;

import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.credit.domain.Contract;
import com.ruoyi.credit.service.IContractService;

/**
 * 基础合同管理控制器
 * 
 * 提供基础合同的CRUD操作，操作dc_contract表
 * 
 * @author ruoyi
 * @date 2025-01-11
 */
@RestController
@RequestMapping("/api/credit/base-contracts")
public class ContractController extends BaseController {

    @Autowired
    private IContractService contractService;

    /**
     * 查询基础合同列表
     */
    @PreAuthorize("@ss.hasPermi('credit:contract:list')")
    @GetMapping("/list")
    public TableDataInfo list(Contract contract) {
        startPage();
        List<Contract> list = contractService.selectContractList(contract);
        return getDataTable(list);
    }

    /**
     * 导出基础合同列表
     */
    @PreAuthorize("@ss.hasPermi('credit:contract:export')")
    @Log(title = "基础合同", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Contract contract) {
        List<Contract> list = contractService.selectContractList(contract);
        ExcelUtil<Contract> util = new ExcelUtil<Contract>(Contract.class);
        util.exportExcel(response, list, "基础合同数据");
    }

    /**
     * 获取基础合同详细信息
     */
    @PreAuthorize("@ss.hasPermi('credit:contract:query')")
    @GetMapping(value = "/{contractId}")
    public AjaxResult getInfo(@PathVariable("contractId") Long contractId) {
        return success(contractService.selectContractByContractId(contractId));
    }

    /**
     * 新增基础合同
     */
    @PreAuthorize("@ss.hasPermi('credit:contract:add')")
    @Log(title = "基础合同", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody Contract contract) {
        Contract result = contractService.createContract(contract);
        return success(result);
    }

    /**
     * 修改基础合同
     */
    @PreAuthorize("@ss.hasPermi('credit:contract:edit')")
    @Log(title = "基础合同", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody Contract contract) {
        return toAjax(contractService.updateContract(contract));
    }

    /**
     * 删除基础合同
     */
    @PreAuthorize("@ss.hasPermi('credit:contract:remove')")
    @Log(title = "基础合同", businessType = BusinessType.DELETE)
    @DeleteMapping("/{contractIds}")
    public AjaxResult remove(@PathVariable Long[] contractIds) {
        return toAjax(contractService.deleteContractByContractIds(contractIds));
    }

    /**
     * 确认合同
     */
    @PreAuthorize("@ss.hasPermi('credit:contract:confirm')")
    @Log(title = "基础合同", businessType = BusinessType.UPDATE)
    @PutMapping("/{contractId}/confirm")
    public AjaxResult confirmContract(@PathVariable("contractId") Long contractId) {
        try {
            boolean result = contractService.confirmContract(contractId, "SECURITY_COMPANY");
            return toAjax(result);
        } catch (Exception e) {
            logger.error("确认合同失败", e);
            return error("确认合同失败: " + e.getMessage());
        }
    }

    /**
     * 更新合同状态
     */
    @PreAuthorize("@ss.hasPermi('credit:contract:edit')")
    @Log(title = "基础合同", businessType = BusinessType.UPDATE)
    @PutMapping("/{contractId}/status/{status}")
    public AjaxResult updateStatus(@PathVariable("contractId") Long contractId, 
                                  @PathVariable("status") String status) {
        try {
            boolean result = contractService.updateContractStatus(contractId, status);
            return toAjax(result);
        } catch (Exception e) {
            logger.error("更新合同状态失败", e);
            return error("更新合同状态失败: " + e.getMessage());
        }
    }
}