package com.ruoyi.applet.contoller;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.model.domain.AppletOutDate;
import com.ruoyi.model.domain.AppletServiceLog;
import com.ruoyi.model.service.IAppletOutDateService;
import com.ruoyi.model.service.IAppletServiceLogService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.time.ZoneId;
import java.util.Date;
import java.util.List;

@Api(description = "接单地址-不接单日期相关接口")
@RestController
@RequestMapping("/applet/outDate")
public class ApiAppletOutDateController {
    @Autowired
    private IAppletOutDateService appletOutDateService;


    @ApiOperation("接单地址-不接单日期列表数据查询")
    @GetMapping("/outDateList")
    public AjaxResult outDateList(AppletOutDate appletOutDate){
        List<AppletOutDate> dateList = appletOutDateService.selectAppletOutDateList(appletOutDate);
        return AjaxResult.success("不接单日期列表数据",dateList);
    }

    @ApiOperation("接单地址-不接单日期添加")
    @PostMapping("/insertOutDate")
    public AjaxResult insertOutDate(@RequestBody AppletOutDate appletOutDate){
        //地址标识必须输入
        if(appletOutDate.getAddressId() == null){
            return AjaxResult.error("地址标识必须输入");
        }
        //删除之前的不接单日期信息
        appletOutDateService.deleteAppletOutDateByIdList(appletOutDate.getAddressId());




//        //不接单日期必须输入
//        if(appletOutDate.getDataJson() == null){
//            return AjaxResult.error("不接单日期必须输入");
//        }
        //得到不接单日期数据,用split截取批量增加
        String[] split = appletOutDate.getDataJson().split(",");
        for (int i = 0; i < split.length; i++) {
            AppletOutDate appletOutDate1 = new AppletOutDate();
            appletOutDate1.setAddressId(appletOutDate.getAddressId());
            //把字符串2025-01-05转换为date类型
            LocalDate localDate = LocalDate.parse(split[i]);
            Date date = Date.from(localDate.atStartOfDay(ZoneId.systemDefault()).toInstant());
            appletOutDate1.setDate(date); // 确保setDate接受Date类型
            appletOutDateService.insertAppletOutDate(appletOutDate1);
        }
        return AjaxResult.success();
    }

    @ApiOperation("接单地址-不接单日期修改")
    @PostMapping("/udpateOutDate")
    public AjaxResult udpateOutDate(@RequestBody AppletOutDate appletOutDate){
        return toAjax(appletOutDateService.updateAppletOutDate(appletOutDate));
    }

    @ApiOperation("接单地址-不接单日期删除")
    @PostMapping("/deleteOutDate")
    public AjaxResult deleteOutDate(long id){
        return toAjax(appletOutDateService.deleteAppletOutDateById(id));
    }

    protected AjaxResult toAjax(int rows) {
        return rows > 0 ? AjaxResult.success() : AjaxResult.error();
    }
}
