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.page.TableDataInfo;
import com.bonus.zlpt.common.security.utils.SecurityUtils;
import com.bonus.zlpt.company.api.domain.SysDic;
import com.bonus.zlpt.company.mapper.BmCompanyInfoDao;
import com.bonus.zlpt.company.service.BmCompanyTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;
import java.time.LocalDateTime;
import java.util.List;

/**
 * 企业类型管理
 */
@RestController
@RequestMapping("company_type")
@Api(value = "企业类型管理",tags = {"企业类型管理"})
public class BmCompanyTypeController extends BaseController {


    @Autowired
    private BmCompanyTypeService bmCompanyTypeService;

    @Autowired
    private BmCompanyInfoDao bmCompanyInfoDao;


    /**
     * 根据条件查询企业类型数据
     * @return
     */
    @PostMapping("/selectCompanyTypeList")
    @ApiOperation("根据条件查询企业类型数据")
    public TableDataInfo selectCompanyTypeList(@RequestBody SysDic sysDic){

        startPage(sysDic.getPageNum(),sysDic.getPageSize());
        sysDic.setPId(1);
        List<SysDic> list = bmCompanyTypeService.selectCompanyTypeList(sysDic);
        return getDataTable(list);

    }

    /**
     * 根据条件查询企业所属
     * @return
     */
    @PostMapping("/selectCompanyLtd")
    @ApiOperation("根据条件查询企业所属")
    public TableDataInfo selectCompanyLtd(@RequestBody SysDic sysDic){

        startPage(sysDic.getPageNum(),sysDic.getPageSize());
        sysDic.setPId(5);
        List<SysDic> list = bmCompanyTypeService.selectCompanyTypeList(sysDic);
        return getDataTable(list);

    }

    /**
     * 根据条件查询证件类型
     * @return
     */
    @PostMapping("/selectIdCard")
    @ApiOperation("根据条件查询证件类型")
    public TableDataInfo selectIdCard(@RequestBody SysDic sysDic){

        startPage(sysDic.getPageNum(),sysDic.getPageSize());
        sysDic.setPId(23);
        List<SysDic> list = bmCompanyTypeService.selectCompanyTypeList(sysDic);
        return getDataTable(list);

    }

    /**
     * 新增或者修改企业类型
     * @param sysDic
     * @return
     */
    @PostMapping("/addOrUpdateCompanyType")
    @ApiOperation("新增或者修改企业类型")
    public AjaxResult addOrUpdateCompanyType(@Validated @RequestBody SysDic sysDic ){

        //修改
        if(sysDic.getId()!=null){
            //如果是禁用
            if("0".equals(sysDic.getStatus())){
                return bmCompanyInfoDao.selectCountByCompanyType(sysDic.getName()) > 0
                        ? error("当前企业类型正在使用，不可以禁用")
                        : success(bmCompanyTypeService.updateCompanyType(sysDic));
            }
            return success(bmCompanyTypeService.updateCompanyType(sysDic));
        }

        //封装属性
        sysDic.setPId(1);
        sysDic.setLevel("2");
        sysDic.setStatus("1");
        sysDic.setCreator(SecurityUtils.getUsername());
        sysDic.setCreateTime(LocalDateTime.now().toString());
        //新增
        return success(bmCompanyTypeService.addCompanyType(sysDic));
    }


    /**
     * 根据id删除企业类型
     * @param id
     * @return
     */
    @DeleteMapping("/deleteCompanyType/{id}")
    @ApiOperation("根据id删除企业类型")
    public AjaxResult deleteCompanyType(@PathVariable("id") Integer id){

        //根据id查询企业类型数据
        SysDic sysDic = bmCompanyTypeService.getById(id);
        if(sysDic==null){
            return error("数据不存在");
        }

        return bmCompanyInfoDao.selectCountByCompanyType(sysDic.getName()) > 0
                ? error("当前企业类型正在使用，不可以删除")
                : success(bmCompanyTypeService.deleteCompanyType(id));

    }


    /**
     * 企业类型导出
     * @param response
     */
    @GetMapping("/exportExcel")
    @ApiOperation("企业类型导出")
    public void exportExcel(HttpServletResponse response) throws UnsupportedEncodingException {

        ExcelUtil<SysDic> sysDicExcelUtil = new ExcelUtil<>(SysDic.class);
        sysDicExcelUtil.exportExcel(response,bmCompanyTypeService.selectCompanyTypeList(new SysDic()),"企业类型","","企业类型");

    }


}
