package com.bs.jxc.controller;

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

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
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.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import cn.hutool.core.lang.Validator;
import com.bs.jxc.domain.CmChargeItem;
import com.bs.jxc.service.ICmChargeItemService;
import javax.annotation.Resource;

/**
 * 费目设置Controller
 *
 * @author bs
 * @date 2024-03-25
 */
@Api(tags = "费目设置")
@RestController
@RequestMapping("/charge/item")
public class CmChargeItemController extends BaseController {
    @Resource
    private ICmChargeItemService cmChargeItemService;

    /**
     * 分页查询费目设置列表
     */
    @ApiOperation("分页查询费目设置列表")
    @GetMapping("/pageList")
    public TableDataInfo pageList(CmChargeItem cmChargeItem) {
        startPage();
        LambdaQueryWrapper<CmChargeItem> queryWrapper = new LambdaQueryWrapper();
        condition(queryWrapper,cmChargeItem);
        List<CmChargeItem> list = cmChargeItemService.list(queryWrapper);
        return getDataTable(list);
    }

    /**
     * 查询费目设置列表
     */
    @ApiOperation("查询费目设置列表")
    @GetMapping("/list")
    public AjaxResult list(CmChargeItem cmChargeItem) {
        LambdaQueryWrapper<CmChargeItem> queryWrapper = new LambdaQueryWrapper();
        condition(queryWrapper,cmChargeItem);
        List<CmChargeItem> list = cmChargeItemService.list(queryWrapper);
        return success(list);
    }

    /**
     * 导出费目设置列表
     */
    @ApiOperation("导出费目设置列表")
    @Log(title = "费目设置导出", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CmChargeItem cmChargeItem) {
        LambdaQueryWrapper<CmChargeItem> queryWrapper = new LambdaQueryWrapper();
        condition(queryWrapper,cmChargeItem);
        List<CmChargeItem> list = cmChargeItemService.list(queryWrapper);
        ExcelUtil<CmChargeItem> util = new ExcelUtil<CmChargeItem>(CmChargeItem. class);
        util.exportExcel(response, list, "费目设置数据");
    }

    /**
     * 获取费目设置详细信息
     */
    @ApiOperation("获取费目设置详细信息")
    @GetMapping(value = "/{itemId}")
    public AjaxResult getInfo(@PathVariable("itemId") String itemId) {
        return success(cmChargeItemService.getById(itemId));
    }

    /**
     * 新增费目设置
     */
    @ApiOperation("新增费目设置")
    @Log(title = "费目设置新增", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CmChargeItem cmChargeItem) {
        return toAjax(cmChargeItemService.save(cmChargeItem));
    }

    /**
     * 修改费目设置
     */
    @ApiOperation("修改费目设置")
    @Log(title = "费目设置修改", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CmChargeItem cmChargeItem) {
        return toAjax(cmChargeItemService.updateById(cmChargeItem));
    }

    /**
     * 删除费目设置
     */
    @ApiOperation("删除费目设置")
    @Log(title = "费目设置删除", businessType = BusinessType.DELETE)
    @DeleteMapping("/{itemIds}")
    public AjaxResult remove(@PathVariable List<Long> itemIds) {
        return toAjax(cmChargeItemService.removeBatchByIds(itemIds));
    }

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


        //主键
        if(Validator.isNotEmpty(cmChargeItem.getItemId())){
            queryWrapper.eq(CmChargeItem::getItemId,cmChargeItem.getItemId());
        }

        //费目代码
        if(Validator.isNotEmpty(cmChargeItem.getItemCode())){
            queryWrapper.eq(CmChargeItem::getItemCode,cmChargeItem.getItemCode());
        }

        //费目名称
        if(Validator.isNotEmpty(cmChargeItem.getItemName())){
            queryWrapper.eq(CmChargeItem::getItemName,cmChargeItem.getItemName());
        }

        //费用类型--数据字典
        if(Validator.isNotEmpty(cmChargeItem.getItemType())){
            queryWrapper.eq(CmChargeItem::getItemType,cmChargeItem.getItemType());
        }

        //计算方式
        if(Validator.isNotEmpty(cmChargeItem.getCalcType())){
            queryWrapper.eq(CmChargeItem::getCalcType,cmChargeItem.getCalcType());
        }

        //说明
        if(Validator.isNotEmpty(cmChargeItem.getREMARK())){
            queryWrapper.eq(CmChargeItem::getREMARK,cmChargeItem.getREMARK());
        }

        //分类
        if(Validator.isNotEmpty(cmChargeItem.getCATEGORY())){
            queryWrapper.eq(CmChargeItem::getCATEGORY,cmChargeItem.getCATEGORY());
        }

        //是否需要开票
        if(Validator.isNotEmpty(cmChargeItem.getNeedInvoice())){
            queryWrapper.eq(CmChargeItem::getNeedInvoice,cmChargeItem.getNeedInvoice());
        }

        //状态
        if(Validator.isNotEmpty(cmChargeItem.getSTATUS())){
            queryWrapper.eq(CmChargeItem::getSTATUS,cmChargeItem.getSTATUS());
        }

        }

}
