package com.bonus.zlpt.company.controller;
import com.bonus.zlpt.common.core.utils.poi.ExcelUtil;
import com.bonus.zlpt.common.core.web.controller.BaseController;
import com.bonus.zlpt.common.core.web.domain.AjaxResult;
import com.bonus.zlpt.common.core.web.domain.BaseEntity;
import com.bonus.zlpt.common.core.web.page.TableDataInfo;
import com.bonus.zlpt.common.log.enums.BusinessType;
import com.bonus.zlpt.common.security.annotation.RequiresPermissions;
import com.bonus.zlpt.common.security.utils.SecurityUtils;
import com.bonus.zlpt.company.api.domain.BmCompanyInfo;
import com.bonus.zlpt.company.api.domain.dto.BmCompanyDto;
import com.bonus.zlpt.company.api.domain.dto.BmCompanyInfoDto;
import com.bonus.zlpt.company.api.domain.vo.BmCompanyInfoVo;
import com.bonus.zlpt.company.mapper.BmCompanyInfoDao;
import com.bonus.zlpt.company.service.BmCompanyInfoService;
import com.bonus.zlpt.common.log.annotation.Log;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;
import java.time.LocalDateTime;
import java.util.List;

/**
* 企业信息表(bm_company_info)表控制层
* @author 阮世耀
*/
@RestController
@RequestMapping("/company_info")
@Api(value = "企业信息管理",tags = {"企业信息表"})
public class BmCompanyInfoController extends BaseController {

    @Resource
    private BmCompanyInfoService bmCompanyInfoService;

    @Resource
    private BmCompanyInfoDao bmCompanyInfoDao;


    /**
    * 通过主键查询单条数据
    * @param id 主键
    * @return 单条数据
    */
    @GetMapping("selectById")
    @ApiOperation("通过主键查询单条数据")
    public BmCompanyInfo selectById(Integer id) { return bmCompanyInfoService.selectByPrimaryKey(id); }


    /**
     * 获取公司列表
     */
    @RequiresPermissions("system:company:list")
    @GetMapping("/list")
    @ApiOperation("获取公司列表")
    public AjaxResult list(BmCompanyInfo obj) {
        List<BmCompanyInfo> list = bmCompanyInfoDao.selectCompanyList(obj);
        return success(list);
    }

    /**
     * 新增 -- 录入企业信息
     */
    @RequiresPermissions("system:company:add")
    @Log(title = "企业管理", businessType = BusinessType.INSERT)
    @PostMapping("addCompanyInfo")
    @ApiOperation("录入企业信息")
    public AjaxResult add(@RequestBody BmCompanyInfoDto obj) {
        return toAjax(bmCompanyInfoService.insertSelective(obj));
    }

    /**
     * 修改 -- 企业信息变更
     */
    @RequiresPermissions("system:company:edit")
    @Log(title = "企业管理", businessType = BusinessType.UPDATE)
    @PostMapping("updateCompanyInfo")
    @ApiOperation("企业信息变更")
    public AjaxResult edit(@RequestBody BmCompanyInfoDto obj) {

        Integer deptId = obj.getBmCompanyInfo().getCompanyId();
        if(!"0".equals(obj.getBmCompanyInfo().getStatus()) && !"3".equals(obj.getBmCompanyInfo().getStatus())){
            //封装审核人以及审核时间
            obj.getBmCompanyInfo().setAuditor(Math.toIntExact(SecurityUtils.getUserId()));
            obj.getBmCompanyInfo().setAuditTime(LocalDateTime.now().toString());
        }
        return toAjax(bmCompanyInfoService.updateByPrimaryKeySelective(obj));
    }

    /**
     * 删除 -- 企业注销
     */
    @RequiresPermissions("system:company:remove")
    @Log(title = "企业管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{deptId}")
    @ApiOperation("企业注销")
    public AjaxResult remove(@PathVariable Integer deptId) {
        return toAjax(bmCompanyInfoService.deleteByPrimaryKey(deptId));
    }


    /**
     * 根据条件查询企业信息(企业入驻审核)
     */
    @PostMapping("/selectList")
    @ApiOperation("根据条件查询企业信息(企业入驻审核)")
    public TableDataInfo selectList(@RequestBody BmCompanyDto bmCompanyDto){

        startPage(bmCompanyDto.getPageNum(), bmCompanyDto.getPageSize());
        List<BmCompanyInfo> list = bmCompanyInfoService.selectList(bmCompanyDto);
        return getDataTable(list);
    }


    /**
     * 根据 企业id 查询企业信息
     * @return
     */
    @GetMapping("/getCompanyInfoById/{id}")
    @ApiOperation("根据企业id查询企业信息")
    public AjaxResult getCompanyInfoById(@PathVariable("id") Integer id){

        return success(bmCompanyInfoService.getCompanyInfoById(id));
    }

    /**
     * 企业入驻信息导出
     * @param response
     */
    @GetMapping("/exportExcel")
    @ApiOperation("企业入驻信息导出")
    public void exportExcel(HttpServletResponse response) throws UnsupportedEncodingException {

        ExcelUtil<BmCompanyInfo> bmCompanyInfoExcelUtil = new ExcelUtil<>(BmCompanyInfo.class);
        bmCompanyInfoExcelUtil.exportExcel(response,bmCompanyInfoService.selectList(new BmCompanyDto()),"企业入驻信息","","企业入驻信息");
    }


    /**
     * 根据条件查询企业详细信息
     * @return
     */
    @PostMapping("/selectCompanyInfo")
    @ApiOperation("根据条件查询企业详细信息")
    public TableDataInfo selectCompanyInfo(@RequestBody BmCompanyDto bmCompanyDto){

        startPage(bmCompanyDto.getPageNum(), bmCompanyDto.getPageSize());
        List<BmCompanyInfoVo> list = bmCompanyInfoService.selectCompanyInfo(bmCompanyDto);
        return getDataTable(list);
    }
}
