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.BaseSupplierVo;
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.core.domain.model.LoginUser;
import com.ozs.common.core.page.TableDataInfo;
import com.ozs.common.enums.BusinessType;
import com.ozs.common.utils.StringUtils;
import com.ozs.base.domain.BaseSupplier;
import com.ozs.base.service.BaseSupplierService;
import com.ozs.framework.web.service.TokenService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
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.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * 供应商管理
 *
 * @author sunhh
 */
@Api(tags = "供应商管理")
@RestController
@RequestMapping("/base/supplier")
public class BaseSupplierController extends BaseController {

    @Autowired
    private BaseSupplierService baseSupplierService;
    @Autowired
    private TokenService tokenService;

    @ApiOperation(value = "新增供应商户", notes = "")
    @PostMapping("/insertBaseSupplier")
    @PreAuthorize("@ss.hasPermi('base:supplier:insertBaseSupplier')")
    @Log(title = ModularConstans.policy, businessType = BusinessType.INSERT)
    public AjaxResult insertBaseSupplier(@RequestBody BaseSupplier baseSupplier, HttpServletRequest request) {
        if (StringUtils.isNull(baseSupplier) || StringUtils.isNull(baseSupplier.getSupplierName())) {
            return error("供应商名称不能为空");
        }
        //获取登录人
        LoginUser loginUser = tokenService.getLoginUser(request);
        baseSupplier.setCreated(String.valueOf(loginUser.getUserId()));
        baseSupplier.setCreateTime(new Date());
        return toAjax(baseSupplierService.insertBaseSupplier(baseSupplier));
    }

    @ApiOperation(value = "删除供应商户", notes = "必传ID")
    @PostMapping("/deleteBaseSupplier")
    @PreAuthorize("@ss.hasPermi('base:supplier:deleteBaseSupplier')")
    @Log(title = ModularConstans.policy, businessType = BusinessType.DELETE)
    public AjaxResult deleteBaseSupplier(@RequestBody BaseSupplier baseSupplier) {
        if (StringUtils.isNull(baseSupplier) || StringUtils.isNull(baseSupplier.getId())) {
            return error("删除ID不能为空");
        }
        return toAjax(baseSupplierService.deleteBaseSupplier(baseSupplier.getId()));
    }

    @ApiOperation(value = "修改供应商户", notes = "必传ID及修改字段")
    @PostMapping("/updateBaseSupplier")
    @PreAuthorize("@ss.hasPermi('base:supplier:updateBaseSupplier')")
    @Log(title = ModularConstans.policy, businessType = BusinessType.UPDATE)
    public AjaxResult updateBaseSupplier(@RequestBody BaseSupplier baseSupplier, HttpServletRequest request) {
        if (StringUtils.isNull(baseSupplier) || StringUtils.isNull(baseSupplier.getId())) {
            return error("修改数据及ID不能为空");
        }
        //获取登录人
        LoginUser loginUser = tokenService.getLoginUser(request);
        baseSupplier.setUpdated(String.valueOf(loginUser.getUserId()));
        baseSupplier.setUpdateTime(new Date());
        return toAjax(baseSupplierService.updateBaseSupplier(baseSupplier));
    }

    @ApiOperation(value = "预先采购开关", notes = "必传id,supplierAdvancePurchase 其他字段不传； 关闭传0，开启传1")
    @PostMapping("/updateBaseSupplierAdvancePurchase")
    public AjaxResult updateBaseSupplierAdvancePurchase(@RequestBody BaseSupplier baseSupplier) {
        if (StringUtils.isNull(baseSupplier) || StringUtils.isNull(baseSupplier.getId())
                || StringUtils.isNull(baseSupplier.getSupplierAdvancePurchase())) {
            return error("预先采购及ID不能为空");
        }
        return toAjax(baseSupplierService.updateBaseSupplier(baseSupplier));
    }

    @ApiOperation(value = "黑白名单开关", notes = "必传id,supplierState 其他字段不传； 黑名单传0，白名单传1")
    @PostMapping("/updateSupplierType")
    public AjaxResult updateSupplierType(@RequestBody BaseSupplier baseSupplier) {
        if (StringUtils.isNull(baseSupplier) || StringUtils.isNull(baseSupplier.getId())
                || StringUtils.isNull(baseSupplier.getSupplierState())) {
            return error("状态及ID不能为空");
        }
        return toAjax(baseSupplierService.updateBaseSupplier(baseSupplier));
    }

    @ApiOperation(value = "查询供应商户", notes = "非必传 查询条件：供应商名称，供应商类型，供应商状态，预先采购状态")
    @PostMapping("/selectBaseSupplier")
    @PreAuthorize("@ss.hasPermi('base:supplier:selectBaseSupplier')")
    @Log(title = ModularConstans.policy, businessType = BusinessType.QUERY)
    public AjaxResult selectBaseSupplier(@RequestBody BaseSupplierVo baseSupplierVo) {
//        List<BaseSupplier> baseSupplierList = baseSupplierService.selectBaseSupplier(baseSupplier);
//        return getDataTable(baseSupplierList);
        LambdaQueryWrapper<BaseSupplier> lw = new LambdaQueryWrapper<BaseSupplier>();
        if (!StringUtils.isBlank(baseSupplierVo.getSupplierName())) {
            lw.like(BaseSupplier::getSupplierName, "%" + baseSupplierVo.getSupplierName() + "%");
        }
        if (!StringUtils.isBlank(baseSupplierVo.getSupplierState())) {
            lw.eq(BaseSupplier::getSupplierState, baseSupplierVo.getSupplierState());
        }
        if (!StringUtils.isBlank(baseSupplierVo.getSupplierType())) {
            lw.eq(BaseSupplier::getSupplierType, baseSupplierVo.getSupplierType());
        }
        if (!StringUtils.isBlank(baseSupplierVo.getSupplierAdvancePurchase())) {
            lw.eq(BaseSupplier::getSupplierAdvancePurchase, baseSupplierVo.getSupplierAdvancePurchase());
        }
        IPage<BaseSupplier> page = baseSupplierService.page(new Page<BaseSupplier>(baseSupplierVo.getPageNum(), baseSupplierVo.getPageSize()), lw);
        return success(page);
    }
}
