package com.weather.controller;


import com.weather.common.AjaxResult;
import com.weather.common.BaseController;
import com.weather.common.page.TableDataInfo;
import com.weather.entity.WeaRotaInfo;
import com.weather.service.IWeaRotaInfoService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.security.SecureRandom;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import static com.weather.Util.dateTimeUtil.findDates;


/**
 * <p>
 * 值班信息 前端控制器
 * </p>
 *
 * @author feng
 * @since 2023-09-05
 */
@RestController
@RequestMapping("weaRotaInfo")
public class WeaRotaInfoController extends BaseController {

    @Autowired
    private IWeaRotaInfoService weaRotaInfoService;

    /**
     * 获取值班信息列表
     */
    @ApiOperation(value = "获取值班信息列表")
    @GetMapping("/list")
    public TableDataInfo list(WeaRotaInfo weaRotaInfo)
    {
        startPage();
        List<WeaRotaInfo> list = weaRotaInfoService.selectWeaRotaInfoList(weaRotaInfo);
        return getDataTable(list);
    }

    /**
     * 根据值班信息编号获取详细信息
     */
    @ApiOperation(value = "根据编号获取详细信息")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable Long id)
    {
        return success(weaRotaInfoService.selectWeaRotaInfoById(id));
    }

    /**
     * 根据时间段获取值班信息
     */
    @ApiOperation(value = "根据时间段获取值班信息")
    @GetMapping(value = "/{startDate}/{endDate}")
    public AjaxResult getInfo(@PathVariable Date startDate ,@PathVariable Date endDate)
    {


        if (startDate==null || endDate ==null)
        {
            return error("查询日期不能为空");
        }else {

            long long1 =startDate.getTime();
            long long2= endDate.getTime();
            if (long2 < long1)
            {
                return error( "结束日期不能小于开始日期");
            }

        }

        List<WeaRotaInfo> list = weaRotaInfoService.selectWeaRotaInfoByDate(startDate,endDate);
        return success(list);
    }

    /**
     * 新增值班信息
     */

    @ApiOperation(value = "新增值班信息")
    @PostMapping
    public AjaxResult add(@Validated @RequestBody WeaRotaInfo weaRotaInfo)
    {   int toAjaxResult=0;

       String rotaDates= weaRotaInfo.getRotaDate();
       String[] rotaDate=rotaDates.split(",");

        String startDate = rotaDate[0];
        String endDate = rotaDate[1];
        //集合中包含2019-05-01/2019-05-05，不需要可去除
        List<String> list = new ArrayList<>();
        try {
            list = findDates(startDate, endDate);

        } catch (ParseException e) {
            e.printStackTrace();
        }
        for(String date : list) {

            SecureRandom secureRandom = new SecureRandom();
            long randomInt = secureRandom.nextInt(100000000);
            weaRotaInfo.setId( randomInt);
            weaRotaInfo.setRotaDate(date);
            toAjaxResult=  weaRotaInfoService.insertWeaRotaInfo(weaRotaInfo);

        }



        return toAjax(toAjaxResult);
    }

    /**
     * 修改值班信息
     */

    @ApiOperation(value = "修改值班信息")
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody WeaRotaInfo weaRotaInfo)
    {

        return toAjax(weaRotaInfoService.updateWeaRotaInfo(weaRotaInfo));
    }

    /**
     * 删除值班信息
     */

    @ApiOperation(value = "删除值班信息")
    @DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable Long id)
    {
        return toAjax(weaRotaInfoService.deleteWeaRotaInfoById(id));
    }

}

