package com.ozs.web.controller.base;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ozs.base.domain.BaseExpert;
import com.ozs.base.domain.vo.BaseExpertVo;
import com.ozs.base.service.BaseExpertService;
import com.ozs.common.annotation.Log;
import com.ozs.common.constant.ModularConstans;
import com.ozs.common.core.controller.BaseController;
import com.ozs.common.core.domain.AjaxResult;
import com.ozs.common.enums.BusinessType;
import com.ozs.common.utils.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.NotEmpty;

/**
 * 专家库管理
 *
 * @author sunhh
 */
@Api(tags = "专家库管理")
@RestController
@RequestMapping("/base/expert")
public class BaseExpertController extends BaseController {

    @Autowired
    private BaseExpertService baseExpertService;

    @ApiOperation(value = "新增专家库", notes = "必传 专家库名称")
    @PostMapping("/insertExpert")
    @PreAuthorize("@ss.hasPermi('base:expert:insertExpert')")
    @Log(title = ModularConstans.policy, businessType = BusinessType.INSERT)
    public AjaxResult insertExpert(@RequestBody BaseExpert baseExpert) {
        if (StringUtils.isNull(baseExpert) || StringUtils.isNull(baseExpert.getExpertName())) {
            return error("专家库名称不能为空");
        }
        return toAjax(baseExpertService.insertExpert(baseExpert));
    }

    @ApiOperation(value = "删除专家库", notes = "必传 id")
    @PostMapping("/deleteExpert")
    public AjaxResult deleteExpert(@RequestBody BaseExpert baseExpert) {
        if (StringUtils.isNull(baseExpert) || StringUtils.isNull(baseExpert.getId())) {
            return error("专家库id不能为空");
        }
        return toAjax(baseExpertService.removeById(baseExpert.getId()));
    }

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

    @ApiOperation(value = "查询专家库", notes = "非必传 查询条件：品目名称")
    @PostMapping("/selectExpert")
    public AjaxResult selectExpert(@RequestBody BaseExpertVo baseExpertVo) {
        LambdaQueryWrapper<BaseExpert> lw = new LambdaQueryWrapper<BaseExpert>();
        if (!StringUtils.isBlank(baseExpertVo.getExpertNameVo())) {
            lw.like(BaseExpert::getExpertName, baseExpertVo.getExpertNameVo());
        }
        if (!StringUtils.isBlank(baseExpertVo.getMajorTypeVo())) {
            lw.eq(BaseExpert::getMajorType, baseExpertVo.getMajorTypeVo());
        }
        if (!ObjectUtils.isEmpty(baseExpertVo.getMajorGradeVo())) {
            lw.eq(BaseExpert::getMajorGrade, baseExpertVo.getMajorGradeVo());
        }
        IPage<BaseExpert> page = baseExpertService.page(new Page<BaseExpert>(baseExpertVo.getPageNum(), baseExpertVo.getPageSize()), lw);
        return success(page);
    }

    @ApiOperation(value = "黑白名单开关", notes = "必传id,status 其他字段不传； 黑名单传0，白名单传1")
    @PostMapping("/updateSupplierType")
    public AjaxResult updateSupplierType(@RequestBody BaseExpert baseExpert) {
        if (StringUtils.isNull(baseExpert) || StringUtils.isNull(baseExpert.getId())
                || StringUtils.isNull(baseExpert.getStatus())) {
            return error("状态及ID不能为空");
        }
//        LambdaQueryWrapper<BaseExpert> lw = new LambdaQueryWrapper<BaseExpert>();
//        if (!StringUtils.isNull(baseExpert.getId())) {
//            lw.eq(BaseExpert::getId, baseExpert.getId());
//        }
//        if (!StringUtils.isBlank(baseExpert.getStatus())) {
//            lw.eq(BaseExpert::getStatus, baseExpert.getStatus());
//        }
        return toAjax(baseExpertService.updateSupplierType(baseExpert));
    }
}
