package com.bs.df.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.df.domain.DfBrokerProduct;
import com.bs.df.service.IDfBrokerProductService;
import javax.annotation.Resource;

/**
 * 代理产品权限Controller
 *
 * @author bs
 * @date 2024-04-06
 */
@Api(tags = "代理产品权限")
@RestController
@RequestMapping("/broker/product")
public class DfBrokerProductController extends BaseController {
    @Resource
    private IDfBrokerProductService dfBrokerProductService;

    /**
     * 分页查询代理产品权限列表
     */
    @ApiOperation("分页查询代理产品权限列表")
    @GetMapping("/pageList")
    public TableDataInfo pageList(DfBrokerProduct dfBrokerProduct) {
        startPage();
        LambdaQueryWrapper<DfBrokerProduct> queryWrapper = new LambdaQueryWrapper();
        condition(queryWrapper,dfBrokerProduct);
        List<DfBrokerProduct> list = dfBrokerProductService.list(queryWrapper);
        return getDataTable(list);
    }

    /**
     * 查询代理产品权限列表
     */
    @ApiOperation("查询代理产品权限列表")
    @GetMapping("/list")
    public AjaxResult list(DfBrokerProduct dfBrokerProduct) {
        LambdaQueryWrapper<DfBrokerProduct> queryWrapper = new LambdaQueryWrapper();
        condition(queryWrapper,dfBrokerProduct);
        List<DfBrokerProduct> list = dfBrokerProductService.list(queryWrapper);
        return success(list);
    }

    /**
     * 导出代理产品权限列表
     */
    @ApiOperation("导出代理产品权限列表")
    @Log(title = "代理产品权限导出", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, DfBrokerProduct dfBrokerProduct) {
        LambdaQueryWrapper<DfBrokerProduct> queryWrapper = new LambdaQueryWrapper();
        condition(queryWrapper,dfBrokerProduct);
        List<DfBrokerProduct> list = dfBrokerProductService.list(queryWrapper);
        ExcelUtil<DfBrokerProduct> util = new ExcelUtil<DfBrokerProduct>(DfBrokerProduct. class);
        util.exportExcel(response, list, "代理产品权限数据");
    }

    /**
     * 获取代理产品权限详细信息
     */
    @ApiOperation("获取代理产品权限详细信息")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(dfBrokerProductService.getById(id));
    }

    /**
     * 新增代理产品权限
     */
    @ApiOperation("新增代理产品权限")
    @Log(title = "代理产品权限新增", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody DfBrokerProduct dfBrokerProduct) {
        return toAjax(dfBrokerProductService.save(dfBrokerProduct));
    }

    /**
     * 修改代理产品权限
     */
    @ApiOperation("修改代理产品权限")
    @Log(title = "代理产品权限修改", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody DfBrokerProduct dfBrokerProduct) {
        return toAjax(dfBrokerProductService.updateById(dfBrokerProduct));
    }

    /**
     * 删除代理产品权限
     */
    @ApiOperation("删除代理产品权限")
    @Log(title = "代理产品权限删除", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable List<Long> ids) {
        return toAjax(dfBrokerProductService.removeBatchByIds(ids));
    }

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


        //id
        if(Validator.isNotEmpty(dfBrokerProduct.getId())){
            queryWrapper.eq(DfBrokerProduct::getId,dfBrokerProduct.getId());
        }

        //代理id
        if(Validator.isNotEmpty(dfBrokerProduct.getBrokerId())){
            queryWrapper.eq(DfBrokerProduct::getBrokerId,dfBrokerProduct.getBrokerId());
        }

        //产品id
        if(Validator.isNotEmpty(dfBrokerProduct.getProductId())){
            queryWrapper.eq(DfBrokerProduct::getProductId,dfBrokerProduct.getProductId());
        }

        //备注
        if(Validator.isNotEmpty(dfBrokerProduct.getRemark())){
            queryWrapper.eq(DfBrokerProduct::getRemark,dfBrokerProduct.getRemark());
        }

        //创建部门
        if(Validator.isNotEmpty(dfBrokerProduct.getCreateDept())){
            queryWrapper.eq(DfBrokerProduct::getCreateDept,dfBrokerProduct.getCreateDept());
        }

        //创建人员
        if(Validator.isNotEmpty(dfBrokerProduct.getCreateBy())){
            queryWrapper.eq(DfBrokerProduct::getCreateBy,dfBrokerProduct.getCreateBy());
        }

        //创建时间
        if(Validator.isNotEmpty(dfBrokerProduct.getCreateTime())){
            queryWrapper.eq(DfBrokerProduct::getCreateTime,dfBrokerProduct.getCreateTime());
        }

        //修改人员
        if(Validator.isNotEmpty(dfBrokerProduct.getUpdateBy())){
            queryWrapper.eq(DfBrokerProduct::getUpdateBy,dfBrokerProduct.getUpdateBy());
        }

        //修改时间
        if(Validator.isNotEmpty(dfBrokerProduct.getUpdateTime())){
            queryWrapper.eq(DfBrokerProduct::getUpdateTime,dfBrokerProduct.getUpdateTime());
        }

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

        //租户id
        if(Validator.isNotEmpty(dfBrokerProduct.getTenantId())){
            queryWrapper.eq(DfBrokerProduct::getTenantId,dfBrokerProduct.getTenantId());
        }

        }

}
