package org.jeecg.modules.adweb.enquiry.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.common.util.FastJsonUtil;
import org.jeecg.modules.adweb.enquiry.entity.AdwebEnquiryRecord;
import org.jeecg.modules.adweb.enquiry.mapper.AdwebEnquiryRecordMapper;
import org.jeecg.modules.adweb.enquiry.service.IAdwebEnquiryRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * @Description: adweb_enquiry_record
 * @Author: jeecg-boot
 * @Date: 2021-05-11
 * @Version: V1.0
 */
@Tag(name = "adweb_enquiry_record")
@RestController
@RequestMapping("/adweb/adwebEnquiryRecord")
@Slf4j
public class AdwebEnquiryRecordController extends JeecgController<AdwebEnquiryRecord, IAdwebEnquiryRecordService> {

    @Autowired
    private IAdwebEnquiryRecordService adwebEnquiryRecordService;

    @Resource
    private AdwebEnquiryRecordMapper adwebEnquiryRecordMapper;

    /**
     * 分页列表查询
     *
     * @param adwebEnquiryRecord
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @AutoLog(value = "adweb_enquiry_record-分页列表查询")
    @Operation(summary = "adweb_enquiry_record-分页列表查询")
    @GetMapping(value = "/list")
    public Result<?> queryPageList(AdwebEnquiryRecord adwebEnquiryRecord,
                                   @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                   @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                   HttpServletRequest req) {
        QueryWrapper<AdwebEnquiryRecord> queryWrapper = QueryGenerator.initQueryWrapper(adwebEnquiryRecord, req.getParameterMap());
        Page<AdwebEnquiryRecord> page = new Page<AdwebEnquiryRecord>(pageNo, pageSize);
        IPage<AdwebEnquiryRecord> pageList = adwebEnquiryRecordService.page(page, queryWrapper);
        return Result.OK(pageList);
    }

    /**
     * @param adwebEnquiryRecord
     * @return
     */
    @GetMapping(value = "/enquiryRecordList")
    public Result<?> enquiryRecordList(AdwebEnquiryRecord adwebEnquiryRecord) {
        QueryWrapper<AdwebEnquiryRecord> queryWrapper = new QueryWrapper<>();
        List<AdwebEnquiryRecord> pageList = adwebEnquiryRecordMapper.getEnquiryRecord(adwebEnquiryRecord);
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();

        return Result.OK(pageList);
    }

    /**
     * 添加
     *
     * @param adwebEnquiryRecord
     * @return
     */
    @AutoLog(value = "adweb_enquiry_record-添加")
    @Operation(summary = "adweb_enquiry_record-添加")
    @PostMapping(value = "/add")
    public Result<?> add(AdwebEnquiryRecord adwebEnquiryRecord) {
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();

        log.info("adwebEnquiryRecord:{}", FastJsonUtil.toJSONString(adwebEnquiryRecord));
        adwebEnquiryRecord.setCtime(new Date());
        adwebEnquiryRecord.setStatus(1);
        adwebEnquiryRecord.setUid(sysUser.getId());
        adwebEnquiryRecordService.save(adwebEnquiryRecord);
        return Result.OK("添加成功！");
    }

    /**
     * 编辑
     *
     * @param adwebEnquiryRecord
     * @return
     */
    @AutoLog(value = "adweb_enquiry_record-编辑")
    @Operation(summary = "adweb_enquiry_record-编辑")
    @PutMapping(value = "/edit")
    public Result<?> edit(@RequestBody AdwebEnquiryRecord adwebEnquiryRecord) {
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();

        adwebEnquiryRecordService.updateById(adwebEnquiryRecord);
        return Result.OK("编辑成功!");
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "adweb_enquiry_record-通过id删除")
    @Operation(summary = "adweb_enquiry_record-通过id删除")
    @DeleteMapping(value = "/delete")
    public Result<?> delete(@RequestParam(name = "id", required = true) String id) {
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();

        adwebEnquiryRecordService.removeById(id);
        return Result.OK("删除成功!");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "adweb_enquiry_record-批量删除")
    @Operation(summary = "adweb_enquiry_record-批量删除")
    @DeleteMapping(value = "/deleteBatch")
    public Result<?> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();

        this.adwebEnquiryRecordService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.OK("批量删除成功!");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @AutoLog(value = "adweb_enquiry_record-通过id查询")
    @Operation(summary = "adweb_enquiry_record-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<?> queryById(@RequestParam(name = "id", required = true) String id) {
        AdwebEnquiryRecord adwebEnquiryRecord = adwebEnquiryRecordService.getById(id);
        if (adwebEnquiryRecord == null) {
            return Result.error("未找到对应数据");
        }
        return Result.OK(adwebEnquiryRecord);
    }

    /**
     * 导出excel
     *
     * @param request
     * @param adwebEnquiryRecord
     */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, AdwebEnquiryRecord adwebEnquiryRecord) {
        return super.exportXls(request, adwebEnquiryRecord, AdwebEnquiryRecord.class, "adweb_enquiry_record");
    }

    /**
     * 通过excel导入数据
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();

        return super.importExcel(request, response, AdwebEnquiryRecord.class);
    }

}
