package com.bs.jxc.controller;

import cn.hutool.core.lang.Validator;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.bs.common.annotation.Log;
import com.bs.common.core.controller.BaseController;
import com.bs.common.core.domain.AjaxResult;
import com.bs.common.core.page.TableDataInfo;
import com.bs.common.enums.BusinessType;
import com.bs.common.utils.poi.ExcelUtil;
import com.bs.jxc.domain.CkBillNumber;
import com.bs.jxc.service.ICkBillNumberService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

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

/**
 * 单据编号表Controller
 *
 * @author bs
 * @date 2023-09-04
 */
@Api(tags = "单据编号表")
@RestController
@RequestMapping("/bill/number")
public class CkBillNumberController extends BaseController {
    @Resource
    private ICkBillNumberService ckBillNumberService;

    /**
     * 分页查询单据编号表列表
     */
    @ApiOperation("分页查询单据编号表列表")
    @GetMapping("/pageList")
    public TableDataInfo pageList(CkBillNumber ckBillNumber) {
        startPage();
        LambdaQueryWrapper<CkBillNumber> queryWrapper = new LambdaQueryWrapper();
        condition(queryWrapper,ckBillNumber);
        List<CkBillNumber> list = ckBillNumberService.list(queryWrapper);
        return getDataTable(list);
    }

    /**
     * 查询单据编号表列表
     */
    @ApiOperation("查询单据编号表列表")
    @GetMapping("/list")
    public AjaxResult list(CkBillNumber ckBillNumber) {
        LambdaQueryWrapper<CkBillNumber> queryWrapper = new LambdaQueryWrapper();
        condition(queryWrapper,ckBillNumber);
        List<CkBillNumber> list = ckBillNumberService.list(queryWrapper);
        return success(list);
    }

    /**
     * 导出单据编号表列表
     */
    @ApiOperation("导出单据编号表列表")
    @Log(title = "单据编号表导出", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CkBillNumber ckBillNumber) {
        LambdaQueryWrapper<CkBillNumber> queryWrapper = new LambdaQueryWrapper();
        condition(queryWrapper,ckBillNumber);
        List<CkBillNumber> list = ckBillNumberService.list(queryWrapper);
        ExcelUtil<CkBillNumber> util = new ExcelUtil<CkBillNumber>(CkBillNumber. class);
        util.exportExcel(response, list, "单据编号表数据");
    }

    /**
     * 获取单据编号表详细信息
     */
    @ApiOperation("获取单据编号表详细信息")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(ckBillNumberService.getById(id));
    }

    /**
     * 新增单据编号表
     */
    @ApiOperation("新增单据编号表")
    @Log(title = "单据编号表新增", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CkBillNumber ckBillNumber) {
        return toAjax(ckBillNumberService.save(ckBillNumber));
    }

    /**
     * 修改单据编号表
     */
    @ApiOperation("修改单据编号表")
    @Log(title = "单据编号表修改", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CkBillNumber ckBillNumber) {
        return toAjax(ckBillNumberService.updateById(ckBillNumber));
    }

    /**
     * 删除单据编号表
     */
    @ApiOperation("删除单据编号表")
    @Log(title = "单据编号表删除", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable List<Long> ids) {
        return toAjax(ckBillNumberService.removeBatchByIds(ids));
    }


    @ApiOperation("获取单据编号")
    @GetMapping("/getBillNo/{billPrefix}")
    public AjaxResult getBillNo(@PathVariable("billPrefix") String billPrefix) {
        String billNumber = ckBillNumberService.getBillNumber(billPrefix);
        return AjaxResult.success("获取成功", billNumber);
    }

    /**
      * 条件设置
      */
    private void condition (LambdaQueryWrapper<CkBillNumber> queryWrapper,CkBillNumber ckBillNumber){


        //ID
        if(null != ckBillNumber.getId()){
            queryWrapper.eq(CkBillNumber::getId,ckBillNumber.getId());
        }

        //单据前缀
        if(Validator.isNotEmpty(ckBillNumber.getBillPrefix())){
            queryWrapper.eq(CkBillNumber::getBillPrefix,ckBillNumber.getBillPrefix());
        }

        //单据日期
        if(Validator.isNotEmpty(ckBillNumber.getBillDate())){
            queryWrapper.eq(CkBillNumber::getBillDate,ckBillNumber.getBillDate());
        }

        //单据流水号
        if(null != ckBillNumber.getBillNumber()) {
            queryWrapper.eq(CkBillNumber::getBillNumber,ckBillNumber.getBillNumber());
        }

        //删除标志（0代表存在 2代表删除）
        if(Validator.isNotEmpty(ckBillNumber.getDelFlag())){
            queryWrapper.eq(CkBillNumber::getDelFlag,ckBillNumber.getDelFlag());
        }

        //创建部门
        if(null != ckBillNumber.getCreateDept()){
            queryWrapper.eq(CkBillNumber::getCreateDept,ckBillNumber.getCreateDept());
        }

        //创建人
        if(null != ckBillNumber.getCreateBy()){
            queryWrapper.eq(CkBillNumber::getCreateBy,ckBillNumber.getCreateBy());
        }

        //创建时间
        if(null != ckBillNumber.getCreateTime()){
            queryWrapper.eq(CkBillNumber::getCreateTime,ckBillNumber.getCreateTime());
        }

        //修改人
        if(null != ckBillNumber.getUpdateBy()){
            queryWrapper.eq(CkBillNumber::getUpdateBy,ckBillNumber.getUpdateBy());
        }

        //修改时间
        if(null != ckBillNumber.getUpdateTime()){
            queryWrapper.eq(CkBillNumber::getUpdateTime,ckBillNumber.getUpdateTime());
        }

        //租户ID
        if(Validator.isNotEmpty(ckBillNumber.getTenantId())){
            queryWrapper.eq(CkBillNumber::getTenantId,ckBillNumber.getTenantId());
        }

        }

}
