package com.bs.df.controller;

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

import com.bs.common.annotation.Anonymous;
import com.bs.df.domain.DfBrokerProduct;
import com.bs.df.domain.DfProductInfo;
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.DfProductRecommend;
import com.bs.df.service.IDfProductRecommendService;
import javax.annotation.Resource;

/**
 * 代理产品推荐Controller
 *
 * @author bs
 * @date 2024-09-16
 */
@Api(tags = "代理产品推荐")
@RestController
@RequestMapping("/product/recommend")
public class DfProductRecommendController extends BaseController {
    @Resource
    private IDfProductRecommendService dfProductRecommendService;

    /**
     * 分页查询代理产品推荐列表
     */
    @ApiOperation("分页查询代理产品推荐列表")
    @GetMapping("/pageList")
    public TableDataInfo pageList(DfProductRecommend dfProductRecommend) {
        startPage();
        LambdaQueryWrapper<DfProductRecommend> queryWrapper = new LambdaQueryWrapper();
        condition(queryWrapper,dfProductRecommend);
        List<DfProductRecommend> list = dfProductRecommendService.list(queryWrapper);
        return getDataTable(list);
    }

    /**
     * 查询代理产品推荐列表
     */
    @ApiOperation("查询代理产品推荐列表")
    @GetMapping("/list")
    public AjaxResult list(DfProductRecommend dfProductRecommend) {
        LambdaQueryWrapper<DfProductRecommend> queryWrapper = new LambdaQueryWrapper();
        condition(queryWrapper,dfProductRecommend);
        List<DfProductRecommend> list = dfProductRecommendService.list(queryWrapper);
        return success(list);
    }

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

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

    /**
     * 修改代理产品推荐
     */
    @ApiOperation("修改代理产品推荐")
    @Log(title = "代理产品推荐修改", businessType = BusinessType.UPDATE)
    @PutMapping("/editByProducts")
    public AjaxResult editByProducts(@RequestBody DfProductRecommend dfProductRecommend) {
        for (Long productId : dfProductRecommend.getIds()) {
            DfProductRecommend productRecommend = new DfProductRecommend();
            productRecommend.setProductId(String.valueOf(productId));
            productRecommend.setUserId(dfProductRecommend.getUserId());
            dfProductRecommendService.save(productRecommend);
        }
        return toAjax(true);
    }

    /**
     * 新增代理产品推荐
     */
    @ApiOperation("新增代理产品推荐")
    @Log(title = "代理产品推荐新增", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody DfProductRecommend dfProductRecommend) {
        return toAjax(dfProductRecommendService.save(dfProductRecommend));
    }

    /**
     * 修改代理产品推荐
     */
    @ApiOperation("修改代理产品推荐")
    @Log(title = "代理产品推荐修改", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody DfProductRecommend dfProductRecommend) {
        return toAjax(dfProductRecommendService.updateById(dfProductRecommend));
    }

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

    /**
     * 移除代理产品推荐
     */
    @ApiOperation("移除代理产品推荐")
    @Log(title = "代理产品推荐移除", businessType = BusinessType.DELETE)
    @PostMapping("/delInfoByRecommend")
    public AjaxResult delInfoByRecommend(@RequestBody DfProductRecommend dfProductRecommend) {
        boolean remove = dfProductRecommendService.remove(new LambdaQueryWrapper<DfProductRecommend>().in(DfProductRecommend::getProductId, dfProductRecommend.getIds())
                .eq(DfProductRecommend::getUserId, dfProductRecommend.getUserId()));
        return toAjax(remove);
    }

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


        //主键
        if(Validator.isNotEmpty(dfProductRecommend.getId())){
            queryWrapper.eq(DfProductRecommend::getId,dfProductRecommend.getId());
        }

        //用户id
        if(Validator.isNotEmpty(dfProductRecommend.getUserId())){
            queryWrapper.eq(DfProductRecommend::getUserId,dfProductRecommend.getUserId());
        }

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

        }

}
