package com.zd.airbottle.controller;

import com.zd.airbottle.domain.QpSupplier;
import com.zd.airbottle.domain.dto.QpSupplierDto;
import com.zd.airbottle.domain.vo.QpSupplierVo;
import com.zd.airbottle.service.IQpSupplierService;
import com.zd.common.core.constant.HttpStatus;
import com.zd.common.core.domain.per.PerFun;
import com.zd.common.core.domain.per.PerPrefix;
import com.zd.common.core.exception.ServiceException;
import com.zd.common.core.utils.StringUtils;
import com.zd.common.core.utils.poi.ExcelUtil;
import com.zd.common.core.web.controller.BaseController;
import com.zd.common.core.web.domain.ResultData;
import com.zd.common.core.web.page.TableDataInfo;
import com.zd.common.log.annotation.Log;
import com.zd.common.log.enums.BusinessType;
import com.zd.common.security.annotation.PreAuthorize;
import com.zd.system.api.dto.SysUserDto;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

/**
 * 供应商Controller
 */
@RestController
@Api(tags = "【供应商】")
@RequestMapping("/supplier")
public class QpSupplierController extends BaseController<QpSupplier> {
    @Resource
    private IQpSupplierService qpSupplierService;

    private static final String DESC="descending";

    /**
     * 查询供应商列表
     */
    //@PreAuthorize(hasPermi = "airbottle:supplier:list")
    @PreAuthorize(hasPermi = PerPrefix.MODULE_AIRBOTTLE+PerPrefix.BUSINESS_SUPPLIER+ PerFun.LIST)
    @GetMapping("/list")
    @ApiOperation(value = "查询供应商列表", notes = "此接口默认查询审核通过的供应商，若需查询待审核供应商，请传参数 state=1，, 权限字符：" + PerPrefix.MODULE_AIRBOTTLE + PerPrefix.BUSINESS_SUPPLIER + PerFun.LIST)
    public TableDataInfo<QpSupplierVo> list(QpSupplier qpSupplier) {
        Integer state = qpSupplier.getState();
        if (state == null || state != 0) {
            startPage("audit_time", DESC);
            if (state==null){
                qpSupplier.setState(1);
            }
        } else{
            startPage("create_time", DESC);
        }
        List<QpSupplier> list = qpSupplierService.getList(qpSupplier);
        TableDataInfo<QpSupplier> dataTable = getDataTable(list);
        List<QpSupplierVo> voList = qpSupplierService.getVoList(list);
        TableDataInfo<QpSupplierVo> dataInfo = new TableDataInfo<>();
        BeanUtils.copyProperties(dataTable, dataInfo);
        dataInfo.setRows(voList);
        return dataInfo;
    }

    /**
     * 查询供应商待审核列表
     */
    @PreAuthorize(hasPermi = PerPrefix.MODULE_AIRBOTTLE + PerPrefix.BUSINESS_SUPPLIER + PerFun.LIST)
    @GetMapping("/checkList")
    @ApiOperation(value = "查询供应商待审核列表", notes = "权限字符：" + PerPrefix.MODULE_AIRBOTTLE + PerPrefix.BUSINESS_SUPPLIER + PerFun.LIST)
    public TableDataInfo<QpSupplier> checkList(QpSupplier qpSupplier) {
        qpSupplier.setState(0);
        startPage("create_time", DESC);
        List<QpSupplier> list = qpSupplierService.checkList(qpSupplier);
        return getDataTable(list);
    }

    /**
     * 导出供应商列表
     */
    @ApiOperation(value = "导出供应商列表", notes = "权限字符：" + PerPrefix.MODULE_AIRBOTTLE + PerPrefix.BUSINESS_SUPPLIER + PerFun.EXPORT)
    @PreAuthorize(hasPermi = "airbottle:supplier:export")
    @Log(title = "供应商", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, QpSupplier qpSupplier) throws IOException {
        qpSupplier.setState(1);
        List<QpSupplier> list = qpSupplierService.getList(qpSupplier);
        List<QpSupplierVo> voList = qpSupplierService.getVoList(list);
        ExcelUtil<QpSupplierVo> util = new ExcelUtil<>(QpSupplierVo.class);
        util.exportExcel(response, voList, "供应商数据");
    }

    /**
     * 获取供应商详细信息
     */
    @ApiOperation(value = "获取供应商详细信息", notes = "权限字符：" + PerPrefix.MODULE_AIRBOTTLE + PerPrefix.BUSINESS_SUPPLIER + PerFun.QUERY)
    @PreAuthorize(hasPermi = "airbottle:supplier:query")
    @GetMapping(value = "/{id}")
    public ResultData<QpSupplierVo> getInfo(@PathVariable("id") Long id) {
        return ResultData.success(qpSupplierService.selectQpSupplierVoById(id));
    }

    /**
     * 小程序端根据登录用户获取供应商详细信息
     */
    @ApiOperation(value = "小程序端根据登录用户获取供应商详细信息", notes = "权限字符：" + PerPrefix.MODULE_AIRBOTTLE + PerPrefix.BUSINESS_SUPPLIER + PerFun.QUERY)
    @GetMapping(value = "/detail")
    public ResultData<QpSupplierVo> getDetail() {
        return ResultData.success(qpSupplierService.getDetail());
    }

    /**
     * 新增供应商
     */
    @ApiOperation(value = "新增供应商", notes = "权限字符：" + PerPrefix.MODULE_AIRBOTTLE + PerPrefix.BUSINESS_SUPPLIER + PerFun.ADD)
    @PreAuthorize(hasPermi = "airbottle:supplier:add")
    @Log(title = "供应商", businessType = BusinessType.INSERT)
    @PostMapping("add")
    public ResultData<Boolean> add(@RequestBody @Valid QpSupplierDto qpSupplierDto) {
        return ResultData.result(qpSupplierService.insertQpSupplier(qpSupplierDto));
    }

    /**
     * 供应商注册
     */
    @ApiOperation(value = "供应商注册")
    @Log(title = "供应商", businessType = BusinessType.INSERT)
    @PostMapping("register")
    public ResultData<SysUserDto> register(@RequestBody @Valid QpSupplierDto qpSupplierDto) {
        if (qpSupplierService.register(qpSupplierDto)) {
            return ResultData.result(true, qpSupplierDto.getSysUserDto(), null);
        }
        throw new ServiceException("注册失败");
    }

    /**
     * 新增供应商名称校验
     */
    @ApiOperation(value = "供应商名称校验", notes = "权限字符：" + PerPrefix.MODULE_AIRBOTTLE + PerPrefix.BUSINESS_SUPPLIER + PerFun.CHECK, tags = "type值默认为0，当前仅支持0，2两个类型，0：供应商全称，2：供应商简称")
    @Log(title = "供应商", businessType = BusinessType.INSERT)
    @GetMapping("check")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "供应商名称或简称", required = true, dataTypeClass = String.class),
            @ApiImplicitParam(name = "type", value = "传入的参数类型（1：供应商名称，2：供应商简称，默认供应商名称，可不传）", dataTypeClass = Integer.class, defaultValue = "1")
    })
    public ResultData<Boolean> check(@RequestParam String name, @RequestParam(name = "type", required = false, defaultValue = "1") Integer type) {
        if (StringUtils.isEmpty(name)) {
            throw new ServiceException("企业名称或简称不能为空");
        }
        if (Boolean.TRUE.equals(qpSupplierService.checkName(name, type))) {
            return ResultData.fail(HttpStatus.CONFLICT, "供应商名称已存在");
        }
        return ResultData.success();
    }

    /**
     * 小程序修改供应商
     */
    //@ApiOperation(value = "小程序修改供应商", notes = "权限字符：" + PerPrefix.MODULE_AIRBOTTLE + PerPrefix.BUSINESS_SUPPLIER + PerFun.EDIT)
    //@PreAuthorize(hasPermi = "airbottle:supplier:edit")
    @Log(title = "供应商", businessType = BusinessType.UPDATE)
    @PutMapping
    public ResultData<Boolean> edit(@RequestBody @Valid QpSupplierDto qpSupplierDto) {
        return ResultData.result(qpSupplierService.updateQpSupplier(qpSupplierDto, true));
    }

    /**
     * 管理端修改供应商
     */
    @ApiOperation(value = "管理端修改供应商", notes = "权限字符：" + PerPrefix.MODULE_AIRBOTTLE + PerPrefix.BUSINESS_SUPPLIER + PerFun.EDIT)
    @PreAuthorize(hasPermi = "airbottle:supplier:edit", hasAnyPermi = "system:user:query")
    @Log(title = "供应商", businessType = BusinessType.UPDATE)
    @PutMapping("update")
    public ResultData<Boolean> update(@RequestBody @Valid QpSupplierDto qpSupplierDto) {
        return ResultData.result(qpSupplierService.updateQpSupplier(qpSupplierDto, false));
    }

    /**
     * 供应商启停用
     */
    @ApiOperation(value = "供应商停用", notes = "权限字符：" + PerPrefix.MODULE_AIRBOTTLE + PerPrefix.BUSINESS_SUPPLIER + PerFun.EDIT)
    @PreAuthorize(hasPermi = "airbottle:supplier:stop")
    @Log(title = "供应商", businessType = BusinessType.UPDATE)
    @PutMapping("/stop/{id}/{status}")
    public ResultData<Boolean> stop(@PathVariable("id") Long id, @PathVariable("status") Integer status) {
        return ResultData.result(qpSupplierService.updateQpSupplierStatus(new QpSupplier().setId(id).setStatus(status)));
    }

    /**
     * 删除供应商
     */
    @ApiOperation(value = "删除供应商", notes = "权限字符：" + PerPrefix.MODULE_AIRBOTTLE + PerPrefix.BUSINESS_SUPPLIER + PerFun.REMOVE)
    @PreAuthorize(hasPermi = "airbottle:supplier:remove")
    @Log(title = "供应商", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public ResultData<Boolean> remove(@PathVariable Long[] ids) {
        return ResultData.result(qpSupplierService.deleteQpSupplierByIds(ids));
    }
}
