package com.fs.app.controller;


import cn.hutool.core.bean.BeanUtil;

import com.fs.app.params.vo.CityVO;
import com.fs.app.utils.CityTreeUtil;
import com.fs.common.core.domain.R;
import com.fs.hisStore.domain.FsCityScrm;
import com.fs.hisStore.domain.FsUserAddressScrm;
import com.fs.hisStore.param.FsAddressParseParam;
import com.fs.hisStore.param.FsUserAddressAddEditParam;
import com.fs.hisStore.service.IFsCityScrmService;
import com.fs.hisStore.service.IFsExpressScrmService;
import com.fs.hisStore.service.IFsUserAddressScrmService;
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="/store/app/address")
public class AddressScrmController {

    @Autowired
    private IFsExpressScrmService expressService;
    @Autowired
    private IFsUserAddressScrmService addressService;

    @Autowired
    private IFsCityScrmService cityService;


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

    }*/

    @ApiOperation("获取城市数据")
    @GetMapping("/getCitys")
    @Cacheable("cityData")
    public R getCitys(HttpServletRequest request){
        List<FsCityScrm> list=cityService.selectFsCitys();
        List<CityVO> cityVOS = Lists.newArrayList();
        for (FsCityScrm 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"));

    }

    @GetMapping("/getAddressList")
    public R getAddressList(FsUserAddressScrm fsUserAddress){
        fsUserAddress.setIsDel(0);
        List<FsUserAddressScrm> list = addressService.selectFsUserAddressList(fsUserAddress);
        return R.ok().put("data", list);
    }

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

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

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

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


    @ApiOperation("删除所有地址")
    @PostMapping("/delAllAddress")
    public R delAllAddress(@RequestBody FsUserAddressAddEditParam address){
        Long userId = address.getUserId();
        addressService.delAllAddress(userId);
        return R.ok("操作成功");
    }


}
