package com.fs.app.controller;


import cn.hutool.core.bean.BeanUtil;
import com.fs.app.annotation.Login;
import com.fs.app.utils.CityTreeUtil;
import com.fs.app.vo.CityVO;
import com.fs.common.core.domain.R;
import com.fs.store.domain.FsCity;
import com.fs.store.domain.FsUserAddress;
import com.fs.store.param.FsAddressParseParam;
import com.fs.store.param.FsUserAddressAddEditParam;
import com.fs.store.service.IFsCityService;
import com.fs.store.service.IFsExpressService;
import com.fs.store.service.IFsUserAddressService;
import com.google.common.collect.Lists;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.Cacheable;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;


@Api("地址接口")
@RestController
@RequestMapping(value="/app/address")
public class AddressController extends  AppBaseController {

    @Autowired
    private IFsExpressService expressService;
    @Autowired
    private IFsUserAddressService addressService;

    @Autowired
    private IFsCityService cityService;
    @Login
    @ApiOperation("获取城市数据")
    @GetMapping("/getCity")
    @Cacheable("citys")
    public R getCity(HttpServletRequest request){
        List<FsCity> list=cityService.selectFsCitys();
        return R.ok().put("data", list);

    }

    @Login
    @ApiOperation("获取城市数据")
    @GetMapping("/getCitys")
    @Cacheable("cityData")
    public R getCitys(HttpServletRequest request){
        List<FsCity> list=cityService.selectFsCitys();
        List<CityVO> cityVOS = Lists.newArrayList();
        for (FsCity city : list){
            CityVO cityVO = new CityVO();
            cityVO.setV(city.getCityId());
            cityVO.setN(city.getCityName());
            cityVO.setPid(city.getParentId());
            cityVOS.add(cityVO);
        }
        return R.ok().put("data", CityTreeUtil.list2TreeConverter(cityVOS, "0"));

    }

    @Login
//    @ApiOperation("获取地址列表")
    @GetMapping("/getAddressList")
    public R getAddressList(HttpServletRequest request){
        try {
            FsUserAddress map=new FsUserAddress();
            map.setUserId(Long.parseLong(getUserId()));
            map.setIsDel(0);
            List<FsUserAddress> list=addressService.selectFsUserAddressList(map);
            return R.ok().put("data",list);
        } catch (Exception e){
            return R.error("操作异常");
        }
    }
    @Login
//    @ApiOperation("获取地址信息")
    @GetMapping("/getAddressById")
    public R getAddressById(@RequestParam("id")Long id, HttpServletRequest request){
        FsUserAddress address=addressService.selectFsUserAddressById(id);
        if(!address.getUserId().equals(Long.parseLong(getUserId()))){

            return R.error("非法操作");
        }
        return R.ok().put("data",address);
    }
    @Login
    @ApiOperation("添加地址")
    @PostMapping("/addAddress")
    public R addAddress(@Valid @RequestBody FsUserAddressAddEditParam address, HttpServletRequest request){
        //处理地址数量 最大20个
        Integer count=addressService.selectFsUserAddressCountsByUserId(Long.parseLong(getUserId()));
        if(count>20){
            return R.error("最多可创建20个地址");
        }
        address.setUserId(Long.parseLong(getUserId()));
        if(address.getIsDefault()==1){
            //处理默认地址
            addressService.clearIsDefalut(Long.parseLong(getUserId()));
        }
        address.setPhone(address.getPhone().trim());
        FsUserAddress userAddress=new FsUserAddress();
        BeanUtil.copyProperties(address, userAddress);
        addressService.insertFsUserAddress(userAddress);
        return R.ok("操作成功");
    }

    @Login
    @ApiOperation("编辑地址")
    @PostMapping("/editAddress")
    public R editAddress(@Valid @RequestBody FsUserAddressAddEditParam address, HttpServletRequest request){
        if(address.getIsDefault()==1){
            //处理默认地址
            addressService.clearIsDefalut(Long.parseLong(getUserId()));
        }
        FsUserAddress userAddress=new FsUserAddress();
        BeanUtil.copyProperties(address, userAddress);
        addressService.updateFsUserAddress(userAddress);
        return R.ok("操作成功");
    }

    @Login
    @ApiOperation("删除地址")
    @PostMapping("/delAddress")
    public R delAddress(@RequestBody FsUserAddressAddEditParam address, HttpServletRequest request){
        addressService.deleteFsUserAddressById(address.getId());
        return R.ok("操作成功");
    }

    @Login
    @ApiOperation("地址解析")
    @PostMapping("/parseAddress")
    public R parseAddress(@RequestBody FsAddressParseParam param){
        return expressService.parseAddress(param.getContent());
    }


    @Login
    @ApiOperation("删除所有地址")
    @PostMapping("/delAllAddress")
    public R delAllAddress( HttpServletRequest request){
        addressService.delAllAddress(Long.parseLong(getUserId()));
        return R.ok("操作成功");
    }

}
