package com.zhyc.module.sale.controller;

import java.util.List;
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.*;
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;

@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());
            // 拼接完整地址
            exportItem.setFullAddress(
                    (customer.getProvince() != null ? customer.getProvince() : "") +
                            (customer.getCity() != null ? customer.getCity() : "") +
                            (customer.getDistrict() != null ? customer.getDistrict() : "") +
                            (customer.getAddress() != null ? customer.getAddress() : "")
            );
            exportItem.setRemark(customer.getRemark());
            return exportItem;
        }).collect(Collectors.toList());

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

    @PreAuthorize("@ss.hasPermi('customer:customer:query')")
    @GetMapping("/{id}")
    public AjaxResult getInfo(@PathVariable 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());
        return toAjax(sxCustomerService.insertSxCustomer(sxCustomer));
    }

    @PreAuthorize("@ss.hasPermi('customer:customer:edit')")
    @Log(title = "客户管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SxCustomer sxCustomer) {
        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));
    }
}