package com.ozs.web.controller.base;

import com.ozs.base.domain.BaseProfessional;
import com.ozs.base.domain.vo.BaseProfessionalVo;
import com.ozs.base.service.BaseProfessionalService;
import com.ozs.common.core.controller.BaseController;
import com.ozs.common.core.domain.AjaxResult;
import com.ozs.common.core.domain.model.LoginUser;
import com.ozs.common.exception.ServiceException;
import com.ozs.common.utils.StringUtils;
import com.ozs.common.utils.poi.ExcelUtil;
import com.ozs.plan.doman.ProvisionalPlan;
import com.ozs.plan.doman.vo.requestVo.ProvisionalPlanVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

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

/**
 * 专业库管理
 *
 * @author sunhh
 */
@Api(tags = "专业库管理")
@RestController
@RequestMapping("/base/professional")
public class BaseProfessionalController extends BaseController {

    @Autowired
    private BaseProfessionalService baseProfessionalService;

    @ApiOperation(value = "新增专业库", notes = "必传 专业库名称、一级分类、编码、父级编码；没有父级编码传0")
    @PostMapping("/insertProfessional")
    public AjaxResult insertProfessional(@RequestBody BaseProfessional baseProfessional) {
        if (StringUtils.isNull(baseProfessional)
                || StringUtils.isNull(baseProfessional.getProfessionalName())
                || StringUtils.isNull(baseProfessional.getProfessionalCode())
                || StringUtils.isNull(baseProfessional.getProfessionalGrade())
                || StringUtils.isNull(baseProfessional.getParentCode())) {
            return error("专业库名称、编码、一级分类、父级ID 不能为空");
        }
        return toAjax(baseProfessionalService.save(baseProfessional));
    }

    @ApiOperation(value = "删除专业库", notes = "必传 id")
    @PostMapping("/deleteProfessional")
    public AjaxResult deleteProfessional(@RequestBody BaseProfessional baseProfessional) {
        if (StringUtils.isNull(baseProfessional) || StringUtils.isNull(baseProfessional.getId())) {
            return error("专业库id不能为空");
        }
        return toAjax(baseProfessionalService.removeById(baseProfessional.getId()));
    }

    @ApiOperation(value = "批量删除专业库", notes = "必传 idList")
    @PostMapping("/deleteIds")
    public AjaxResult deleteByIdLIst(@RequestBody BaseProfessionalVo baseProfessionalVo) {
        baseProfessionalService.deleteIds(baseProfessionalVo.getDeleteIds());
        return success();
    }

    @ApiOperation(value = "修改专业库", notes = "必传 id 及修改数据")
    @PostMapping("/updateProfessional")
    public AjaxResult updateProfessional(@RequestBody BaseProfessional baseProfessional) {
        if (StringUtils.isNull(baseProfessional) || StringUtils.isNull(baseProfessional.getId())) {
            return error("专业库id和修改数据不能为空");
        }
        return toAjax(baseProfessionalService.updateById(baseProfessional));
    }

    @ApiOperation(value = "查询专业库树结构", notes = "非必传 查询条件：品目名称")
    @PostMapping("/selectBaseProfessional")
    public AjaxResult selectBaseProfessional(@RequestBody BaseProfessionalVo baseProfessionalVo) {
        List<BaseProfessionalVo> baseSupplierList = baseProfessionalService.selectBaseProfessionalVo(baseProfessionalVo);
        return success(baseSupplierList);
    }

    @ApiOperation(value = "导入专业库", notes = "导入表格")
    @PostMapping("/importBaseProfessional")
    public AjaxResult importBaseProfessional(MultipartFile file, boolean updateSupport) throws Exception {
        ExcelUtil<BaseProfessional> util = new ExcelUtil<>(BaseProfessional.class);
        List<BaseProfessional> baseProfessionalList = util.importExcel(file.getInputStream());
        if (StringUtils.isNull(baseProfessionalList) || baseProfessionalList.size() == 0) {
            throw new ServiceException("导入专业库数据不能为空！");
        }
        // String operName = getUsername();
        String message = baseProfessionalService.importBaseProfessional(baseProfessionalList, updateSupport);
        return success(message);
    }

    @ApiOperation("导出专业库")
    @PostMapping("/exportBaseProfessional")
    public void exportBaseProfessional(HttpServletResponse response, @RequestBody BaseProfessionalVo baseProfessionalVo) {
        List<BaseProfessional> list = baseProfessionalService.selectBaseProfessional(baseProfessionalVo);
        ExcelUtil<BaseProfessional> util = new ExcelUtil<>(BaseProfessional.class);
        util.exportExcel(response, list, "专业库数据");
    }
}
