package com.taais.mf.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.taais.common.core.core.domain.CommonResult;
import com.taais.common.excel.utils.ExcelUtil;
import com.taais.common.log.annotation.Log;
import com.taais.common.log.enums.BusinessType;
import com.taais.common.orm.core.page.TableDataInfo;
import com.taais.common.web.annotation.RepeatSubmit;
import com.taais.common.web.core.BaseController;
import com.taais.mf.domain.bo.CustomerBo;
import com.taais.mf.domain.vo.CustomerVo;
import com.taais.mf.service.ICustomerService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
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 java.util.List;

/**
 * 客户主表Controller
 *
 * @author wgk
 * 2024-01-06
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/mf/customer")
public class CustomerController extends BaseController {
    @Resource
    private ICustomerService customerService;

    /**
     * 查询客户主表列表
     */
    @SaCheckPermission("mf:customer:list")
    @GetMapping("/list")
    public TableDataInfo<CustomerVo> list(CustomerBo customerBo) {
        return customerService.selectPage(customerBo);
    }

    /**
     * 导出客户主表列表
     */
    @SaCheckPermission("mf:customer:export")
    @Log(title = "客户主表", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CustomerBo customerBo) {
        List<CustomerVo> list = customerService.selectList(customerBo);
        ExcelUtil.exportExcel(list, "客户主表", CustomerVo.class, response);
    }

    /**
     * 获取客户主表详细信息
     */
    @SaCheckPermission("mf:customer:query")
    @GetMapping(value = "/{customerId}")
    public CommonResult<CustomerVo> getInfo(@PathVariable Long customerId) {
        return CommonResult.success(customerService.selectById(customerId));
    }

    /**
     * 新增客户主表
     */
    @SaCheckPermission("mf:customer:add")
    @Log(title = "客户主表", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping
    public CommonResult<Void> add(@Validated @RequestBody CustomerBo customerBo) {
        boolean inserted = customerService.insert(customerBo);
        if (!inserted) {
            return CommonResult.fail("新增客户主表记录失败！");
        }
        return CommonResult.success();
    }

    /**
     * 修改客户主表
     */
    @SaCheckPermission("mf:customer:edit")
    @Log(title = "客户主表", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping
    public CommonResult<Void> edit(@Validated @RequestBody CustomerBo customerBo) {
        Boolean updated = customerService.update(customerBo);
        if (!updated) {
            return CommonResult.fail("修改客户主表记录失败!");
        }
        return CommonResult.success();
    }

    /**
     * 删除客户主表
     */
    @SaCheckPermission("mf:customer:remove")
    @Log(title = "客户主表", businessType = BusinessType.DELETE)
    @DeleteMapping("/{customerIds}")
    public CommonResult<Void> remove(@PathVariable Long[] customerIds) {
        boolean deleted = customerService.deleteByIds(customerIds);
        if (!deleted) {
            return CommonResult.fail("删除客户主表记录失败!");
        }
        return CommonResult.success();
    }
}
