package com.ruoyi.cms.controller.cms;


import com.ruoyi.cms.service.IAppUserService;
import com.ruoyi.cms.service.ICompanyCollectionService;
import com.ruoyi.cms.util.RoleUtils;
import com.ruoyi.common.annotation.BussinessLog;
import com.ruoyi.common.core.domain.entity.AppUser;
import com.ruoyi.common.core.domain.entity.Company;
import com.ruoyi.cms.service.ICompanyService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 公司Controller
 * 
 * @author lishundong
 * @date 2024-09-04
 */
@RestController
@RequestMapping("/cms/company")
@Api(tags = "后台：公司管理")
public class CompanyController extends BaseController
{
    @Autowired
    private ICompanyService companyService;
    @Autowired
    private ICompanyCollectionService companyCollectionService;
    @Autowired
    private IAppUserService appUserService;

    /**
     * 查询公司列表
     */
    @ApiOperation("查询公司列表")
//    @PreAuthorize("@ss.hasPermi('cms:company:list')")
    @GetMapping("/list")
    public TableDataInfo list(Company company)
    {
        if (RoleUtils.isCompanyAdmin()) {
            System.out.println("企业社会信用代码============================="+RoleUtils.getCurrentUseridCard());
            company.setCode(RoleUtils.getCurrentUseridCard());
        }
        startPage();
        List<Company> list = companyService.selectCompanyList(company);
        return getDataTable(list);
    }

    /**
     * 导出公司列表
     */
    @ApiOperation("导出公司列表")
//    @PreAuthorize("@ss.hasPermi('app:company:export')")
    @Log(title = "公司", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Company company)
    {
        List<Company> list = companyService.selectCompanyList(company);
        ExcelUtil<Company> util = new ExcelUtil<Company>(Company.class);
        util.exportExcel(response, list, "公司数据");
    }

    /**
     * 获取公司详细信息
     */
    @ApiOperation("获取公司详细信息")
//    @PreAuthorize("@ss.hasPermi('app:company:query')")
    @GetMapping(value = "/{companyId}")
    public AjaxResult getInfo(@PathVariable("companyId") Long companyId)
    {
        return success(companyService.selectCompanyByCompanyId(companyId));
    }

    /**
     * 新增公司
     */
    @ApiOperation("新增公司")
//    @PreAuthorize("@ss.hasPermi('app:company:add')")
    @Log(title = "公司", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Company company)
    {
        return toAjax(companyService.insertCompany(company));
    }

    /**
     * 修改公司
     */
    @ApiOperation("修改公司")
//    @PreAuthorize("@ss.hasPermi('app:company:edit')")
    @Log(title = "公司", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Company company)
    {
        return toAjax(companyService.updateCompany(company));
    }

    /**
     * 删除公司
     */
    @ApiOperation("删除公司")
//    @PreAuthorize("@ss.hasPermi('app:company:remove')")
    @Log(title = "公司", businessType = BusinessType.DELETE)
    @DeleteMapping("/{companyIds}")
    public AjaxResult remove(@PathVariable Long[] companyIds)
    {
        return toAjax(companyService.deleteCompanyByCompanyIds(companyIds));
    }

    @ApiOperation("企业资质审核列表")
    @PreAuthorize("@ss.hasPermi('app:company:approval:list')")
    @GetMapping("/approval/list")
    public TableDataInfo approvalList(Company company)
    {
        startPage();
        List<Company> list = companyService.approvalList(company);
        return getDataTable(list);
    }
    @ApiOperation("企业资质修改")
//    @PreAuthorize("@ss.hasPermi('app:company:approval:edit')")
    @GetMapping("/approval/edit")
    public AjaxResult approvalEdit(Company company)
    {
        startPage();
        List<Company> list = companyService.approvalList(company);
        return AjaxResult.success();
    }

    @ApiOperation("查询公司列表")
//    @PreAuthorize("@ss.hasPermi('cms:company:list')")
    @GetMapping("/listPage")
    public TableDataInfo listPage(Company company)
    {
        startPage();
        List<Company> list = companyService.selectCompanyList(company);
        return getDataTable(list);
    }

    @GetMapping("/queryCodeCompany")
    @ApiOperation("根据社会信用代码查询企业")
    public AjaxResult queryCodeCompany(@RequestParam("code") String code)
    {
        if (!StringUtils.hasText(code)) {
            return AjaxResult.error("社会信用代码不能为空");
        }
        return AjaxResult.success(companyService.queryCodeCompany(code));
    }

    /**
     * 用户收藏公司
     */
    @BussinessLog(title = "用户收藏公司")
    @PostMapping("/collection/{companyId}")
    @ApiOperation("用户收藏公司")
    public AjaxResult companyCollection(@PathVariable("companyId") Long companyId)
    {
        Long userId=null;
        if(SecurityUtils.isLogin()){
            String idCard=RoleUtils.getCurrentUseridCard();
            AppUser appUser=appUserService.selectAppuserByIdcard(idCard);
            if(appUser==null){
                return AjaxResult.error("用户信息未完善,请完善身份证信息!");
            }else{
                userId=appUser.getUserId();
            }
        }else{
            return AjaxResult.error("用户未登录!");
        }
        return toAjax(companyCollectionService.companySysCollection(companyId,userId));
    }

    /**
     * 用户取消收藏公司
     */
    @BussinessLog(title = "用户取消收藏公司")
    @DeleteMapping("/collection/{companyId}")
    @ApiOperation("用户取消收藏公司")
    public AjaxResult companyCancel(@PathVariable("companyId") Long companyId)
    {
        Long userId;
        if(SecurityUtils.isLogin()){
            String idCard=RoleUtils.getCurrentUseridCard();
            AppUser appUser=appUserService.selectAppuserByIdcard(idCard);
            if(appUser==null){
                return AjaxResult.error("用户信息未完善,请完善身份证信息!");
            }else{
                userId=appUser.getUserId();
            }
        }else{
            return AjaxResult.error("用户未登录!");
        }
        return toAjax(companyCollectionService.companySysCancel(companyId,userId));
    }
}
