package com.zhyc.module.sale.controller;

import java.util.List;
import java.util.Date; // 【修改点1】新增导入Date包
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.zhyc.common.annotation.Log;
import com.zhyc.common.core.controller.BaseController;
import com.zhyc.common.core.domain.AjaxResult;
import com.zhyc.common.enums.BusinessType;
import com.zhyc.module.sale.domain.SxCustomer;
import com.zhyc.module.sale.domain.SxCustomerExport;
import com.zhyc.module.sale.service.ISxCustomerService;
import com.zhyc.common.utils.poi.ExcelUtil;
import com.zhyc.common.core.page.TableDataInfo;

/**
 * 客户管理Controller
 *
 * @author ruoyi
 * @date 2025-08-18
 */
@RestController
@RequestMapping("/customer/customer")
public class SxCustomerController extends BaseController {
    @Autowired
    private ISxCustomerService sxCustomerService;

    /**
     * 查询客户管理列表
     */
    @PreAuthorize("@ss.hasPermi('customer:customer:list')")
    @GetMapping("/list")
    public TableDataInfo list(SxCustomer sxCustomer) {
        startPage();
        List<SxCustomer> list = sxCustomerService.selectSxCustomerList(sxCustomer);
        return getDataTable(list);
    }

    /**
     * 导出客户管理列表
     */
    @PreAuthorize("@ss.hasPermi('customer:customer:export')")
    @Log(title = "客户管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SxCustomer sxCustomer) {
        List<SxCustomer> list = sxCustomerService.selectSxCustomerList(sxCustomer);

        // 转换为导出DTO
        List<SxCustomerExport> exportList = list.stream().map(customer -> {
            SxCustomerExport exportItem = new SxCustomerExport();
            exportItem.setName(customer.getName());
            exportItem.setPhone(customer.getPhone());

            // 拼接所在地区 (省 + 市 + 区)
            String area = (customer.getProvince() != null ? customer.getProvince() : "") +
                    (customer.getCity() != null ? customer.getCity() : "") +
                    (customer.getDistrict() != null ? customer.getDistrict() : "");
            exportItem.setArea(area);

            // 详细地址
            exportItem.setAddress(customer.getAddress());

            // 创建人 (从BaseEntity获取)
            exportItem.setCreateBy(customer.getCreateBy());

            // 创建日期 (从BaseEntity获取)
            exportItem.setCreateTime(customer.getCreateTime());

            return exportItem;
        }).collect(Collectors.toList());

        ExcelUtil<SxCustomerExport> util = new ExcelUtil<>(SxCustomerExport.class);
        util.exportExcel(response, exportList, "客户管理数据");
    }

    /**
     * 获取客户管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('customer:customer:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(sxCustomerService.selectSxCustomerById(id));
    }

    /**
     * 新增客户管理
     */
    @PreAuthorize("@ss.hasPermi('customer:customer:add')")
    @Log(title = "客户管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SxCustomer sxCustomer) {
        // 自动填充当前登录用户的 userId 和 deptId
        sxCustomer.setUserId(getUserId());
        sxCustomer.setDeptId(getDeptId());

        // 自动填充创建人
        sxCustomer.setCreateBy(getUsername());

        // 【修改点2】自动填充创建时间为当前时间
        sxCustomer.setCreateTime(new Date());

        return toAjax(sxCustomerService.insertSxCustomer(sxCustomer));
    }

    /**
     * 修改客户管理
     */
    @PreAuthorize("@ss.hasPermi('customer:customer:edit')")
    @Log(title = "客户管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SxCustomer sxCustomer) {
        // 自动填充更新人
        sxCustomer.setUpdateBy(getUsername());

        // 【修改点3】自动填充更新时间为当前时间
        sxCustomer.setUpdateTime(new Date());

        return toAjax(sxCustomerService.updateSxCustomer(sxCustomer));
    }

    /**
     * 删除客户管理
     */
    @PreAuthorize("@ss.hasPermi('customer:customer:remove')")
    @Log(title = "客户管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(sxCustomerService.deleteSxCustomerByIds(ids));
    }
}