package com.fs.company.controller.company;

import java.util.List;

import cn.hutool.core.util.ObjectUtil;
import com.fs.common.core.domain.R;
import com.fs.common.utils.ServletUtils;
import com.fs.company.param.CompanyDomainBindParam;
import com.fs.company.vo.CompanyDomainBindVo;
import com.fs.framework.security.LoginUser;
import com.fs.framework.service.TokenService;
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.fs.common.annotation.Log;
import com.fs.common.core.controller.BaseController;
import com.fs.common.core.domain.AjaxResult;
import com.fs.common.enums.BusinessType;
import com.fs.company.domain.CompanyDomainBind;
import com.fs.company.service.ICompanyDomainBindService;
import com.fs.common.utils.poi.ExcelUtil;
import com.fs.common.core.page.TableDataInfo;

/**
 * 域名绑定销售公司Controller
 * 
 * @author fs
 * @date 2025-06-17
 */
@RestController
@RequestMapping("/company/companyDomainBind")
public class CompanyDomainBindController extends BaseController
{
    @Autowired
    private TokenService tokenService;
    @Autowired
    private ICompanyDomainBindService companyDomainBindService;

    /**
     * 查询域名绑定销售公司列表
     */
    @PreAuthorize("@ss.hasPermi('company:companyDomainBind:list')")
    @GetMapping("/list")
    public TableDataInfo list(CompanyDomainBindParam companyDomainBind)
    {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        companyDomainBind.setCompanyId(loginUser.getUser().getCompanyId());
        startPage();
        List<CompanyDomainBindVo> list = companyDomainBindService.selectCompanyDomainBindList(companyDomainBind);
        return getDataTable(list);
    }

    /**
     * 导出域名绑定销售公司列表
     */
    @PreAuthorize("@ss.hasPermi('company:companyDomainBind:export')")
    @Log(title = "域名绑定销售公司", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(CompanyDomainBindParam companyDomainBind)
    {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        companyDomainBind.setCompanyId(loginUser.getUser().getCompanyId());
        List<CompanyDomainBindVo> list = companyDomainBindService.selectCompanyDomainBindList(companyDomainBind);
        ExcelUtil<CompanyDomainBindVo> util = new ExcelUtil<CompanyDomainBindVo>(CompanyDomainBindVo.class);
        return util.exportExcel(list, "域名绑定销售公司数据");
    }

    /**
     * 获取域名绑定销售公司详细信息
     */
    @PreAuthorize("@ss.hasPermi('company:companyDomainBind:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(companyDomainBindService.selectCompanyDomainBindById(id));
    }

    /**
     * 新增域名绑定销售公司
     */
    @PreAuthorize("@ss.hasPermi('company:companyDomainBind:add')")
    @Log(title = "域名绑定销售公司", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CompanyDomainBind companyDomainBind)
    {
        return toAjax(companyDomainBindService.insertCompanyDomainBind(companyDomainBind));
    }

    /**
     * 修改域名绑定销售公司
     */
    @PreAuthorize("@ss.hasPermi('company:companyDomainBind:edit')")
    @Log(title = "域名绑定销售公司", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CompanyDomainBind companyDomainBind)
    {
        return toAjax(companyDomainBindService.updateCompanyDomainBind(companyDomainBind));
    }

    /**
     * 删除域名绑定销售公司
     */
    @PreAuthorize("@ss.hasPermi('company:companyDomainBind:remove')")
    @Log(title = "域名绑定销售公司", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(companyDomainBindService.deleteCompanyDomainBindByIds(ids));
    }

    /**
     * 域名批量绑定
     **/
    @PreAuthorize("@ss.hasPermi('company:companyDomain:domainBatchBinding')")
    @PostMapping("/domainBatchBinding")
    public R domainBatchBinding(@RequestBody CompanyDomainBindParam param) {
        if (ObjectUtil.isEmpty(param.getCompanyIds()) || param.getCompanyIds().isEmpty()) {
            return R.error("绑定失败，至少有一条域名分配信息！");
        }
        if (ObjectUtil.isEmpty(param.getCompanyUserIds()) || param.getCompanyUserIds().isEmpty()) {
            return R.error("绑定失败，至少有一条分配人员信息！");
        }

        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        param.setUserId(loginUser.getUser().getUserId().toString());
        return companyDomainBindService.domainBatchBinding(param);
    }
}
