package com.fs.company.controller;

import cn.hutool.core.util.IdUtil;
import com.fs.common.annotation.Log;
import com.fs.common.annotation.RepeatSubmit;
import com.fs.common.core.controller.BaseController;
import com.fs.common.core.domain.AjaxResult;
import com.fs.common.core.domain.R;
import com.fs.common.core.domain.model.LoginUser;
import com.fs.common.core.page.TableDataInfo;
import com.fs.common.enums.BusinessType;
import com.fs.common.utils.SecurityUtils;
import com.fs.common.utils.ServletUtils;
import com.fs.common.utils.StringUtils;
import com.fs.common.utils.poi.ExcelUtil;
import com.fs.common.utils.sign.Md5Utils;
import com.fs.company.domain.*;
import com.fs.company.param.CompanyParam;
import com.fs.company.service.*;
import com.fs.company.vo.CompanyOptionRespVO;
import com.fs.company.vo.CompanyVO;
import com.fs.core.utils.OrderCodeUtils;
import com.fs.framework.web.service.TokenService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 企业Controller
 *
 * @author fs
 * @date 2021-10-04
 */
@RestController
@RequestMapping("/company/company")
public class CompanyController extends BaseController
{

    @Autowired
    private ICompanyService companyService;
    @Autowired
    private ICompanyUserService userService;
    /**
     * 查询企业列表
     */
    @PreAuthorize("@ss.hasPermi('company:company:list')")
    @GetMapping("/list")
    public TableDataInfo list(CompanyParam param)
    {
        startPage();
        List<CompanyVO> list = companyService.selectCompanyVOList(param);
        return getDataTable(list);
    }

    /**
     * 导出企业列表
     */
    @PreAuthorize("@ss.hasPermi('company:company:export')")
    @Log(title = "企业", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(CompanyParam company)
    {
        List<CompanyVO> list = companyService.selectCompanyVOList(company);
        ExcelUtil<CompanyVO> util = new ExcelUtil<CompanyVO>(CompanyVO.class);
        return util.exportExcel(list, "company");
    }

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

    /**
     * 新增企业
     */
    @PreAuthorize("@ss.hasPermi('company:company:add')")
    @Log(title = "企业", businessType = BusinessType.INSERT)
    @PostMapping
    public R add(@RequestBody Company company)
    {
        company.setPassword(SecurityUtils.encryptPassword(company.getPassword()));
        company.setAppId(Md5Utils.hash(company.getUserName()));
        company.setAppKey(Md5Utils.hash(company.getPassword()));
        return companyService.insertCompany(company);
    }

    /**
     * 修改企业
     */
    @PreAuthorize("@ss.hasPermi('company:company:edit')")
    @Log(title = "企业", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Company company)
    {
        CompanyUser companyUser = new CompanyUser();
        if (company.getStatus()==0){

        }
        return toAjax(companyService.updateCompany(company));
    }



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

    @GetMapping("/getCompanyList")
    public R getCompanyList()
    {
        Company map=new Company();
        map.setIsDel(0);
        List<Company> list = companyService.selectCompanyList(map);
        return R.ok().put("data",list);
    }





    @PreAuthorize("@ss.hasPermi('company:company:resetPwd')")
    @PostMapping("/resetPwd/{companyId}")
    public AjaxResult resetPwd(@PathVariable Long companyId)
    {
        Company company=companyService.selectCompanyById(companyId);
        return toAjax(userService.resetUserPwdByUserId(company.getUserId(),SecurityUtils.encryptPassword("123456")));
    }


    /**
     * 公司下拉选项（模糊搜索）
     */
    @GetMapping("/option")
    public TableDataInfo option(@RequestParam(required = false) String companyName) {
        List<CompanyOptionRespVO> companyOptionRespVOS = companyService.selectCompanyOptions(companyName);
        return getDataTable(companyOptionRespVOS);
    }


    /**
     * 查询所有企业列表
     */
    @PreAuthorize("@ss.hasPermi('company:company:list')")
    @GetMapping("/listAll")
    public AjaxResult listAll()
    {
        List<CompanyVO> list = companyService.selectCompanyAll();
        return AjaxResult.success(list);
    }





}
