package com.inspect.guideline.controller;

import com.inspect.base.core.utils.poi.ExcelUtil;
import com.inspect.base.core.web.controller.BaseController;
import com.inspect.base.core.web.domain.AjaxResult;
import com.inspect.common.log.annotation.Log;
import com.inspect.common.log.enums.BizType;
import com.inspect.guideline.GuidelineEngineer;
import com.inspect.guideline.domain.PatrolGuideline;
import com.inspect.guideline.service.IPatrolGuidelineService;

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

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping({"/guideline"})
public class PatrolGuidelineController extends BaseController {
    private final IPatrolGuidelineService patrolGuidelineService;
    private final GuidelineEngineer guidelineEngineer;

    public PatrolGuidelineController(IPatrolGuidelineService patrolGuidelineService, GuidelineEngineer guidelineEngineer) {
        this.patrolGuidelineService = patrolGuidelineService;
        this.guidelineEngineer = guidelineEngineer;
    }

    @GetMapping({"/test"})
    public AjaxResult test(PatrolGuideline patrolGuideline) throws ScriptException {
        return AjaxResult.success(this.guidelineEngineer.parse(1L, 50.0D, 0.4D, 5.0D));
    }

    @Log(
            title = "664导则",
            businessType = BizType.EXPORT
    )
    @PostMapping({"/export"})
    public void export(HttpServletResponse response, PatrolGuideline patrolGuideline) {
        List<PatrolGuideline> list = this.patrolGuidelineService.selectPatrolGuidelineList(patrolGuideline);
        ExcelUtil<PatrolGuideline> util = new ExcelUtil<>(PatrolGuideline.class);
        util.exportExcel(response, list, "664导则数据");
    }

    @GetMapping({"/{guidelineId}"})
    public AjaxResult getInfo(@PathVariable("guidelineId") Long guidelineId) {
        return AjaxResult.success(this.patrolGuidelineService.selectPatrolGuidelineByGuidelineId(guidelineId));
    }

    @Log(
            title = "664导则",
            businessType = BizType.INSERT
    )
    @PostMapping
    public AjaxResult add(@RequestBody PatrolGuideline patrolGuideline) {
        return this.toAjax(this.patrolGuidelineService.insertPatrolGuideline(patrolGuideline));
    }

    @Log(
            title = "664导则",
            businessType = BizType.UPDATE
    )
    @PostMapping({"/edit"})
    public AjaxResult edit(@RequestBody PatrolGuideline patrolGuideline) {
        return this.toAjax(this.patrolGuidelineService.updatePatrolGuideline(patrolGuideline));
    }

    @Log(
            title = "664导则",
            businessType = BizType.DELETE
    )
    @PostMapping({"/remove/{guidelineIds}"})
    public AjaxResult remove(@PathVariable Long[] guidelineIds) {
        return this.toAjax(this.patrolGuidelineService.deletePatrolGuidelineByGuidelineIds(guidelineIds));
    }
}
