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.CmCustItem;
import com.bs.jxc.service.ICmCustItemService;
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 2024-02-05
 */
@Api(tags = "客户子项信息")
@RestController
@RequestMapping("/cust/item")
public class CmCustItemController extends BaseController {
    @Resource
    private ICmCustItemService cmCustItemService;

    /**
     * 分页查询客户子项信息列表
     */
    @ApiOperation("分页查询客户子项信息列表")
    @GetMapping("/pageList")
    public TableDataInfo pageList(CmCustItem cmCustItem) {
        startPage();
        LambdaQueryWrapper<CmCustItem> queryWrapper = new LambdaQueryWrapper();
        condition(queryWrapper,cmCustItem);
        List<CmCustItem> list = cmCustItemService.list(queryWrapper);
        return getDataTable(list);
    }

    /**
     * 查询客户子项信息列表
     */
    @ApiOperation("查询客户子项信息列表")
    @GetMapping("/list")
    public AjaxResult list(CmCustItem cmCustItem) {
        LambdaQueryWrapper<CmCustItem> queryWrapper = new LambdaQueryWrapper();
        condition(queryWrapper,cmCustItem);
        List<CmCustItem> list = cmCustItemService.list(queryWrapper);
        return success(list);
    }

    /**
     * 导出客户子项信息列表
     */
    @ApiOperation("导出客户子项信息列表")
    @Log(title = "客户子项信息导出", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CmCustItem cmCustItem) {
        LambdaQueryWrapper<CmCustItem> queryWrapper = new LambdaQueryWrapper();
        condition(queryWrapper,cmCustItem);
        List<CmCustItem> list = cmCustItemService.list(queryWrapper);
        ExcelUtil<CmCustItem> util = new ExcelUtil<CmCustItem>(CmCustItem. class);
        util.exportExcel(response, list, "客户子项信息数据");
    }

    /**
     * 获取客户子项信息详细信息
     */
    @ApiOperation("获取客户子项信息详细信息")
    @GetMapping(value = "/{ID}")
    public AjaxResult getInfo(@PathVariable("ID") Long ID) {
        return success(cmCustItemService.getById(ID));
    }

    /**
     * 新增客户子项信息
     */
    @ApiOperation("新增客户子项信息")
    @Log(title = "客户子项信息新增", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CmCustItem cmCustItem) {
        return toAjax(cmCustItemService.save(cmCustItem));
    }

    /**
     * 修改客户子项信息
     */
    @ApiOperation("修改客户子项信息")
    @Log(title = "客户子项信息修改", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CmCustItem cmCustItem) {
        return toAjax(cmCustItemService.updateById(cmCustItem));
    }

    /**
     * 删除客户子项信息
     */
    @ApiOperation("删除客户子项信息")
    @Log(title = "客户子项信息删除", businessType = BusinessType.DELETE)
    @DeleteMapping("/{IDs}")
    public AjaxResult remove(@PathVariable List<Long> IDs) {
        return toAjax(cmCustItemService.removeBatchByIds(IDs));
    }

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


        //客户子表id
        if(Validator.isNotEmpty(cmCustItem.getID())){
            queryWrapper.eq(CmCustItem::getID,cmCustItem.getID());
        }

        //客户id
        if(Validator.isNotEmpty(cmCustItem.getCustId())){
            queryWrapper.eq(CmCustItem::getCustId,cmCustItem.getCustId());
        }

        //子项代码
        if(Validator.isNotEmpty(cmCustItem.getItemCode())){
            queryWrapper.eq(CmCustItem::getItemCode,cmCustItem.getItemCode());
        }

        //子项值
        if(Validator.isNotEmpty(cmCustItem.getItemValue())){
            queryWrapper.eq(CmCustItem::getItemValue,cmCustItem.getItemValue());
        }

        //排序码
        if(Validator.isNotEmpty(cmCustItem.getSortCode())){
            queryWrapper.eq(CmCustItem::getSortCode,cmCustItem.getSortCode());
        }

        //备注
        if(Validator.isNotEmpty(cmCustItem.getREMARK())){
            queryWrapper.eq(CmCustItem::getREMARK,cmCustItem.getREMARK());
        }

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

        }

}
