package com.yiqi.generator.controller;

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

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.yiqi.common.core.domain.BaseEntity;
import com.yiqi.common.core.domain.R;
import com.yiqi.common.utils.DateUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
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.yiqi.common.annotation.Log;
import com.yiqi.common.core.controller.BaseController;
import com.yiqi.common.core.domain.AjaxResult;
import com.yiqi.common.enums.BusinessType;
import com.yiqi.generator.domain.GenForm;
import com.yiqi.generator.service.IGenFormService;
import com.yiqi.common.utils.poi.ExcelUtil;
import com.yiqi.common.core.page.TableDataInfo;

/**
 * 表单生成记录Controller
 *
 * @author xiaohei
 * @date 2023-07-23
 */
@Api(tags = "表单生成记录")
@RestController
@RequestMapping("generator/form")
public class GenFormController extends BaseController {

    @Autowired
    private IGenFormService genFormService;

    /**
     * 查询表单生成记录列表
     */
    @ApiOperation("查询表单生成记录列表")
    @PreAuthorize("@ss.hasPermi('generator:form:list')")
    @GetMapping("list")
    public TableDataInfo list(GenForm genForm) {
        startPage();
        List<GenForm> list = genFormService.selectGenFormList(genForm);
        return getDataTable(list);
    }

    /**
     * 导出表单生成记录列表
     */
    @ApiOperation("表单生成记录")
    @PreAuthorize("@ss.hasPermi('generator:form:export')")
    @Log(title = "表表单生成记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, GenForm genForm) {
        List<GenForm> list = genFormService.selectGenFormList(genForm);
        ExcelUtil<GenForm> util = new ExcelUtil<GenForm>(GenForm. class);
        util.exportExcel(response, list, "表单生成记录数据");
    }

    /**
     * 获取表表单生成记录详细信息
     */
    @PreAuthorize("@ss.hasPermi('generator:form:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(genFormService.selectGenFormById(id));
    }

    /**
     * 新增表单生成记录
     */
    @PreAuthorize("@ss.hasPermi('generator:form:add')")
    @Log(title = "表单生成记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody GenForm genForm) {
        genForm.setCreateBy(getUsername());
        genForm.setCreateById(getLoginUser().getUserId());
        genForm.setCreateTime(DateUtils.getNowDate());
        return toAjax(genFormService.insertGenForm(genForm));
    }

    /**
     * 修改表单生成记录
     */
    @PreAuthorize("@ss.hasPermi('generator:form:edit')")
    @Log(title = "表单生成记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody GenForm genForm) {
        genForm.setUpdateBy(getUsername());
        genForm.setUpdateById(getLoginUser().getUserId());
        genForm.setUpdateTime(DateUtils.getNowDate());
        return toAjax(genFormService.updateGenForm(genForm));
    }

    @ApiModelProperty("修改表单内容")
    @PostMapping(value = "editDrawingData")
    public R editDrawingData(@RequestBody GenForm genForm){
        genFormService.update(new UpdateWrapper<GenForm>().lambda()
                .set(GenForm::getDrawingData,genForm.getDrawingData())
                .set(BaseEntity::getUpdateById,getLoginUser().getUserId())
                .set(BaseEntity::getUpdateBy,getUsername())
                .set(BaseEntity::getUpdateTime,DateUtils.getNowDate())
                .eq(GenForm::getId,genForm.getId()));
        return R.ok();
    }

    /**
     * 删除表单生成记录
     */
    @PreAuthorize("@ss.hasPermi('generator:form:remove')")
    @Log(title = "表单生成记录", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(genFormService.deleteGenFormByIds(ids));
    }
}
