package com.ruoyi.applet.contoller;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.model.domain.AppletAddress;
import com.ruoyi.model.domain.AppletOutDate;
import com.ruoyi.model.service.IAppletAddressService;
import com.ruoyi.model.service.IAppletOutDateService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(description = "小程序-地址相关接口")
@RestController
@RequestMapping("/applet/address")
public class ApiAppletAddressController {

    @Autowired
    private IAppletAddressService appletAddressService;
    @Autowired
    private IAppletOutDateService appletOutDateService;


    @ApiOperation("地址-地址列表数据查询")
    @GetMapping("/addressList")
    public AjaxResult outDateList(AppletAddress appletAddress){
        if(appletAddress.getUserId() == null){
            return AjaxResult.error("用户标识不能为空");
        }
        List<AppletAddress> addressList = appletAddressService.selectAppletAddressList(appletAddress);
        if(addressList.size()>0){
            //如果地址信息不为空则循环设置不接单日期
            for (AppletAddress appletAddress1 : addressList){
                if(appletAddress1.getId() != null){
                    AppletOutDate appletOutDate = new AppletOutDate();
                    appletOutDate.setAddressId(appletAddress1.getId());
                    List<AppletOutDate> outDateList = appletOutDateService.selectAppletOutDateList(appletOutDate);
                    appletAddress1.setAppletOutDate(outDateList);
                }
            }
        }

        return AjaxResult.success("地址列表数据",addressList);
    }


    //跟胡地址标识查询地址详情
    @ApiOperation("地址-地址详情查询")
    @GetMapping("/addressDetail/{id}")
    public AjaxResult addressDetail( AppletAddress appletAddress){
        AppletAddress address = appletAddressService.selectAppletAddressById(appletAddress.getId());
        if(address.getId() != null){
            AppletOutDate appletOutDate = new AppletOutDate();
            appletOutDate.setAddressId(address.getId());
            List<AppletOutDate> outDateList = appletOutDateService.selectAppletOutDateList(appletOutDate);
            address.setAppletOutDate(outDateList);
        }
        return AjaxResult.success("地址详情",address);
    }








    @ApiOperation("地址-地址添加")
    @PostMapping("/insertAddress")
    public AjaxResult insertOutDate(@RequestBody AppletAddress appletAddress){
        return toAjax(appletAddressService.insertAppletAddress(appletAddress));
    }

    @ApiOperation("地址-地址修改")
    @PostMapping("/udpateAddress")
    public AjaxResult udpateOutDate(@RequestBody AppletAddress appletAddress){
        return toAjax(appletAddressService.updateAppletAddress(appletAddress));
    }

    @ApiOperation("地址-地址删除")
    @PostMapping("/deleteAddress")
    public AjaxResult deleteOutDate(long id){
        return toAjax(appletAddressService.deleteAppletAddressById(id));
    }

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


}
