package com.ruoyi.business.controller;

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

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.business.domain.BusinessSurvey;
import com.ruoyi.business.service.IBusinessSurveyService;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
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.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.business.domain.BusinessTemplate;
import com.ruoyi.business.service.IBusinessTemplateService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import java.util.Arrays;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 模版管理Controller
 * 
 * @author ruoyi
 * @date 2024-03-31
 */
@RestController
@RequestMapping("/business/template")
public class BusinessTemplateController extends BaseController
{
    @Autowired
    private IBusinessTemplateService businessTemplateService;

    /**
     * 查询模版管理列表
     */
    @PreAuthorize("@ss.hasPermi('business:template:list')")
    @GetMapping("/list")
    public TableDataInfo list(BusinessTemplate businessTemplate)
    {
        startPage();
        LambdaQueryWrapper<BusinessTemplate> like = new LambdaQueryWrapper<BusinessTemplate>()
                .eq(StringUtils.isNotBlank( businessTemplate.getOpen()),BusinessTemplate::getOpen, businessTemplate.getOpen())
                .like(StringUtils.isNotBlank(businessTemplate.getTitle()), BusinessTemplate::getTitle, businessTemplate.getTitle());
        if(businessTemplate.getCreateBy() != null){
            logger.debug("SecurityUtils.getUsername():{}",SecurityUtils.getUsername());
            like.eq(BusinessTemplate::getCreateBy, SecurityUtils.getUsername());
        }
        List<BusinessTemplate> list = businessTemplateService.list(like);
        return getDataTable(list);
    }

    /**
     * 查询模版管理列表
     */
    @PreAuthorize("@ss.hasPermi('business:template:list')")
    @GetMapping("/listAll")
    public AjaxResult listAll(BusinessTemplate businessTemplate)
    {
        List<BusinessTemplate> list = businessTemplateService.list(
                new LambdaQueryWrapper<BusinessTemplate>().eq(BusinessTemplate::getCreateBy,SecurityUtils.getUsername())
                .eq(StringUtils.isNotBlank(businessTemplate.getId()),BusinessTemplate::getId,businessTemplate.getId())
        );
        return AjaxResult.success(list);
    }

    /**
     * 导出模版管理列表
     */
    @PreAuthorize("@ss.hasPermi('business:template:export')")
    @Log(title = "模版管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BusinessTemplate businessTemplate)
    {
        List<BusinessTemplate> list = businessTemplateService.list();
        ExcelUtil<BusinessTemplate> util = new ExcelUtil<BusinessTemplate>(BusinessTemplate.class);
        util.exportExcel(response, list, "模版管理数据");
    }

    /**
     * 获取模版管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('business:template:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(businessTemplateService.getById(id));
    }


    /**
     * 新增模版管理
     */
    @PreAuthorize("@ss.hasPermi('business:template:add')")
    @Log(title = "模版管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BusinessTemplate businessTemplate)
    {
        return toAjax(businessTemplateService.save(businessTemplate));
    }

    @Autowired
    private IBusinessSurveyService businessSurveyService;
    /**
     * 修改模版管理
     */
    @PreAuthorize("@ss.hasPermi('business:template:edit')")
    @Log(title = "模版管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BusinessTemplate businessTemplate)
    {
        BusinessSurvey businessSurvey = new BusinessSurvey();
        businessSurvey.setTempName(businessTemplate.getTitle());
        businessSurveyService.lambdaUpdate().eq(BusinessSurvey::getTempId, businessTemplate.getId()).update(businessSurvey);
        return toAjax(businessTemplateService.updateById(businessTemplate));
    }

    /**
     * 删除模版管理
     */
    @PreAuthorize("@ss.hasPermi('business:template:remove')")
    @Log(title = "模版管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(businessTemplateService.removeByIds(Arrays.asList(ids)));
    }
}
