package com.srs.web.controller.stureg;

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

import com.srs.stureg.domain.ArrivalInformation;
import com.srs.stureg.domain.SrsStuTodo;
import com.srs.stureg.service.ISrsStuRegService;
import com.srs.stureg.service.ISrsStuTodoService;
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.srs.common.annotation.Log;
import com.srs.common.core.controller.BaseController;
import com.srs.common.core.domain.AjaxResult;
import com.srs.common.enums.BusinessType;
import com.srs.stureg.domain.AtStation;
import com.srs.stureg.service.IAtStationService;
import com.srs.common.utils.poi.ExcelUtil;
import com.srs.common.core.page.TableDataInfo;

/**
 * 新生接站登记Controller
 *
 * @author 邓
 * @date 2023-05-20
 */
@RestController
@RequestMapping("/stureg/pickstation")
public class AtStationController extends BaseController
{
    @Autowired
    private IAtStationService atStationService;
    @Autowired
    private ISrsStuTodoService srsStuTodoService;
    @Autowired
    private ISrsStuRegService srsStuRegService;
    /**
     * 学生获取到站信息登记详细信息
     */
    @GetMapping("/info")
    public AjaxResult getInfoByUserId()
    {
        AtStation atStation = atStationService.selectAtStationByUserId(getUserId());
        if (atStation !=null){
            return AjaxResult.success(atStation);
        }
        return  AjaxResult.error();
    }
    /**
     * 学生填写到站信息登记
     */
    @Log(title = "到站信息登记", businessType = BusinessType.INSERT)
    @PostMapping("/info")
    public AjaxResult addByStudent(@RequestBody AtStation atStation)
    {
        if (atStationService.selectAtStationByUserId(getUserId())==null){
            atStation.setStudentId(getUserId());
            atStation.setKsh(getUsername());
            SrsStuTodo srsStuTodo = new SrsStuTodo();
            srsStuTodo.setTaskCode("DXDZ");
            srsStuTodo.setKsh(getUsername());
            srsStuTodo.setStatus("1");
            srsStuTodoService.updateSrsStuTodoStastus(srsStuTodo);
            return AjaxResult.success(atStationService.insertAtStation(atStation));
        }
        else {
            atStation.setStudentId(getUserId());
            atStation.setKsh(getUsername());
            SrsStuTodo srsStuTodo = new SrsStuTodo();
            srsStuTodo.setTaskCode("DXDZ");
            srsStuTodo.setKsh(getUsername());
            srsStuTodo.setStatus("1");
            srsStuTodoService.updateSrsStuTodoStastus(srsStuTodo);
            return AjaxResult.success(atStationService.updateAtStation(atStation));
        }
    }

    /**
     * 查询新生接站登记列表
     */
//    @PreAuthorize("@ss.hasPermi('stureg:pickstation:list')")
    @GetMapping("/list")
    public TableDataInfo list(AtStation atStation)
    {
        startPage();
        List<AtStation> list = atStationService.selectAtStationList(atStation);
        return getDataTable(list);
    }

    /**
     * 导出新生接站登记列表
     */
//    @PreAuthorize("@ss.hasPermi('stureg:pickstation:export')")
    @Log(title = "新生接站登记", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, AtStation atStation)
    {
        List<AtStation> list = atStationService.selectAtStationList(atStation);
        ExcelUtil<AtStation> util = new ExcelUtil<AtStation>(AtStation.class);
        util.exportExcel(response, list, "新生接站登记数据");
    }

    /**
     * 获取新生接站登记详细信息
     */
//    @PreAuthorize("@ss.hasPermi('stureg:pickstation:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(atStationService.selectAtStationById(id));
    }

    /**
     * 新增新生接站登记
     */
//    @PreAuthorize("@ss.hasPermi('stureg:pickstation:add')")
    @Log(title = "新生接站登记", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody AtStation atStation)
    {
        return toAjax(atStationService.insertAtStation(atStation));
    }

    /**
     * 修改新生接站登记
     */
//    @PreAuthorize("@ss.hasPermi('stureg:pickstation:edit')")
    @Log(title = "新生接站登记", businessType = BusinessType.UPDATE)
    @PostMapping("/update")
    public AjaxResult edit(@RequestBody AtStation atStation)
    {
        return toAjax(atStationService.updateAtStation(atStation));
    }

    /**
     * 删除新生接站登记
     */
//    @PreAuthorize("@ss.hasPermi('stureg:pickstation:remove')")
    @Log(title = "新生接站登记", businessType = BusinessType.DELETE)
	@PostMapping("/delete/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(atStationService.deleteAtStationByIds(ids));
    }
}
