package com.bonus.material.basic.controller;

import java.util.List;
import java.util.Optional;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotNull;

import com.bonus.common.log.enums.OperaType;
import com.bonus.common.security.utils.SecurityUtils;
import com.bonus.material.common.annotation.PreventRepeatSubmit;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
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.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.bonus.common.log.annotation.SysLog;
import com.bonus.common.security.annotation.RequiresPermissions;
import com.bonus.material.basic.domain.BmCompanyAddress;
import com.bonus.material.basic.service.IBmCompanyAddressService;
import com.bonus.common.core.web.controller.BaseController;
import com.bonus.common.core.web.domain.AjaxResult;
import com.bonus.common.core.utils.poi.ExcelUtil;
import com.bonus.common.core.web.page.TableDataInfo;

/**
 * 企业信息Controller
 *
 * @author xsheng
 * @date 2024-12-16
 */
@Api(tags = "企业信息接口")
@RestController
@RequestMapping("/bm_company_address")
public class BmCompanyAddressController extends BaseController {
    @Autowired
    private IBmCompanyAddressService bmCompanyAddressService;

    /**
     * 查询企业信息列表
     */
    @ApiOperation(value = "查询企业信息列表")
    //@RequiresPermissions("basic:address:list")
    @GetMapping("/list")
    public TableDataInfo list(BmCompanyAddress bmCompanyAddress) {
        startPage();
        bmCompanyAddress.setCompanyId(Optional.ofNullable(SecurityUtils.getLoginUser().getSysUser().getCompanyId()).orElse(SecurityUtils.getLoginUser().getSysUser().getDeptId()));
        List<BmCompanyAddress> list = bmCompanyAddressService.selectBmCompanyAddressList(bmCompanyAddress);
        return getDataTable(list);
    }

    /**
     * 导出企业信息列表
     */
    @ApiOperation(value = "导出企业信息列表")
    @PreventRepeatSubmit
    //@RequiresPermissions("basic:address:export")
    @SysLog(title = "企业信息", businessType = OperaType.EXPORT, logType = 1, module = "仓储管理->导出企业信息")
    @PostMapping("/export")
    public void export(HttpServletResponse response, BmCompanyAddress bmCompanyAddress) {
        List<BmCompanyAddress> list = bmCompanyAddressService.selectBmCompanyAddressList(bmCompanyAddress);
        ExcelUtil<BmCompanyAddress> util = new ExcelUtil<BmCompanyAddress>(BmCompanyAddress.class);
        util.exportExcel(response, list, "企业信息数据");
    }

    /**
     * 获取企业信息详细信息
     */
    @ApiOperation(value = "获取企业信息详细信息")
    //@RequiresPermissions("basic:address:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(bmCompanyAddressService.selectBmCompanyAddressById(id));
    }

    /**
     * 新增企业信息
     */
    @ApiOperation(value = "新增企业信息")
    @PreventRepeatSubmit
    //@RequiresPermissions("basic:address:add")
    @SysLog(title = "企业信息", businessType = OperaType.INSERT, logType = 1, module = "仓储管理->新增企业信息")
    @PostMapping
    public AjaxResult add(@RequestBody BmCompanyAddress bmCompanyAddress) {
        try {
            return toAjax(bmCompanyAddressService.insertBmCompanyAddress(bmCompanyAddress));
        } catch (Exception e) {
            return error("系统错误, " + e.getMessage());
        }
    }

    /**
     * 修改企业信息
     */
    @ApiOperation(value = "修改企业信息")
    //@PreventRepeatSubmit
    //@RequiresPermissions("basic:address:edit")
    @SysLog(title = "企业信息", businessType = OperaType.UPDATE, logType = 1, module = "仓储管理->修改企业信息")
    @PostMapping("/edit")
    public AjaxResult edit(@NotNull @RequestBody BmCompanyAddress bmCompanyAddress) {
        if (bmCompanyAddress == null || bmCompanyAddress.getId() == null) {
            return error("ID不能为空");
        }
        try {
            bmCompanyAddress.setCompanyId(Optional.ofNullable(SecurityUtils.getLoginUser().getSysUser().getCompanyId()).orElse(SecurityUtils.getLoginUser().getSysUser().getDeptId()));
            return toAjax(bmCompanyAddressService.updateBmCompanyAddress(bmCompanyAddress));
        } catch (Exception e) {
            return error("系统错误, " + e.getMessage());
        }
    }

    /**
     * 删除企业信息
     */
    @ApiOperation(value = "删除企业信息")
    @PreventRepeatSubmit
    //@RequiresPermissions("basic:address:remove")
    @SysLog(title = "企业信息", businessType = OperaType.DELETE, logType = 1, module = "仓储管理->删除企业信息")
    @PostMapping("/del/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(bmCompanyAddressService.deleteBmCompanyAddressByIds(ids));
    }


    @ApiOperation(value = "删除企业信息")
    @PreventRepeatSubmit
    //@RequiresPermissions("basic:address:remove")
    @SysLog(title = "企业信息", businessType = OperaType.DELETE, logType = 1, module = "仓储管理->删除企业信息")
    @PostMapping("/delAddress")
    public AjaxResult delAddress(Long id) {
        return toAjax(bmCompanyAddressService.deleteBmCompanyAddressById(id));
    }


}
