package com.hzy.project.business.customer.controller;

import java.util.List;

import com.hzy.common.enums.EDelFlag;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
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.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.hzy.framework.aspectj.lang.annotation.Log;
import com.hzy.framework.aspectj.lang.enums.BusinessType;
import com.hzy.project.business.customer.domain.Customer;
import com.hzy.project.business.customer.service.ICustomerService;
import com.hzy.framework.web.controller.BaseController;
import com.hzy.framework.web.domain.AjaxResult;
import com.hzy.common.utils.poi.ExcelUtil;
import com.hzy.framework.web.page.TableDataInfo;

/**
 * 客户Controller
 * 
 * @author ruoyi
 * @date 2026-01-02
 */
@Controller
@RequestMapping("/business/customer")
public class CustomerController extends BaseController
{
    private String prefix = "business/customer";

    @Autowired
    private ICustomerService customerService;

    @RequiresPermissions("business:customer:view")
    @GetMapping()
    public String customer()
    {
        return prefix + "/customer";
    }

    /**
     * 查询客户列表
     */
    @RequiresPermissions("business:customer:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Customer customer)
    {
        customer.setDelFlag(EDelFlag.NO.getVal());
        startPage();
        List<Customer> list = customerService.selectCustomerList(customer);
        return getDataTable(list);
    }

    /**
     * 导出客户列表
     */
    @RequiresPermissions("business:customer:export")
    @Log(title = "客户", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Customer customer)
    {
        customer.setDelFlag(EDelFlag.NO.getVal());
        List<Customer> list = customerService.selectCustomerList(customer);
        ExcelUtil<Customer> util = new ExcelUtil<Customer>(Customer.class);
        return util.exportExcel(list, "客户数据");
    }

    /**
     * 新增客户
     */
    @RequiresPermissions("business:customer:add")
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存客户
     */
    @RequiresPermissions("business:customer:add")
    @Log(title = "客户", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Customer customer)
    {
        customer.setDelFlag(EDelFlag.NO.getVal());
        return toAjax(customerService.insertCustomer(customer));
    }

    /**
     * 修改客户
     */
    @RequiresPermissions("business:customer:edit")
    @GetMapping("/edit/{customerId}")
    public String edit(@PathVariable("customerId") Long customerId, ModelMap mmap)
    {
        Customer customer = customerService.selectCustomerByCustomerId(customerId);
        mmap.put("customer", customer);
        return prefix + "/edit";
    }

    /**
     * 修改保存客户
     */
    @RequiresPermissions("business:customer:edit")
    @Log(title = "客户", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Customer customer)
    {
        return toAjax(customerService.updateCustomer(customer));
    }

    /**
     * 删除客户
     */
    @RequiresPermissions("business:customer:remove")
    @Log(title = "客户", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(customerService.logicDelCustomerByCustomerIds(ids));
    }
}
