package com.fs.app.controller.store;


import cn.hutool.core.bean.BeanUtil;
import com.fs.app.annotation.Login;
import com.fs.app.controller.AppBaseController;
import com.fs.app.utils.CityTreeUtil;
import com.fs.app.vo.CityVO;
import com.fs.common.core.domain.R;
import com.fs.common.utils.ParseUtils;
import com.fs.his.domain.FsUserAddress;
import com.fs.hisStore.domain.FsCityScrm;
import com.fs.hisStore.domain.FsUserAddressScrm;
import com.fs.hisStore.param.*;
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;

import static com.fs.framework.datasource.DynamicDataSourceContextHolder.log;


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

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

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

    }

    @Login
    @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"));

    }

    @Login
//    @ApiOperation("获取地址列表")
    @GetMapping("/getAddressList")
    public R getAddressList(HttpServletRequest request){
        try {
            FsUserAddressScrm map=new FsUserAddressScrm();
            map.setUserId(Long.parseLong(getUserId()));
            map.setIsDel(0);
            List<FsUserAddressScrm> list=addressService.selectFsUserAddressList(map);
            return R.ok().put("data",list);
        } catch (Exception e){
            log.error(e.getMessage(), e);
            return R.error("操作异常");
        }
    }
    @Login
//    @ApiOperation("获取地址信息")
    @GetMapping("/getAddressById")
    public R getAddressById(@RequestParam("id")Long id, HttpServletRequest request){
        FsUserAddressScrm 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());
        FsUserAddressScrm userAddress=new FsUserAddressScrm();
        BeanUtil.copyProperties(address, userAddress);
        addressService.insertFsUserAddress(userAddress);
        return R.ok("操作成功").put("addressId", userAddress.getId());
    }

    @Login
    @ApiOperation("编辑地址")
    @PostMapping("/editAddress")
    public R editAddress(@Valid @RequestBody FsUserAddressAddEditParam address, HttpServletRequest request){
        if(address.getIsDefault()==1){
            //处理默认地址
            addressService.clearIsDefalut(Long.parseLong(getUserId()));
        }
        FsUserAddressScrm userAddress=new FsUserAddressScrm();
        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("操作成功");
    }

    @Login
    @ApiOperation("获取默认地址信息")
    @GetMapping("/getAddressByDefault")
    public R getAddressByDefault( HttpServletRequest request){
        FsUserAddress address=addressService.selectFsUserAddressByDefault(Long.parseLong(getUserId()));
        if (address!=null&&address.getPhone()!=null&&address.getPhone().length()>11&&!address.getPhone().matches("\\d+")){
            address.setPhone(ParseUtils.parsePhone(address.getPhone()));
        }
        if (null!=address){
            if(!address.getUserId().equals(Long.parseLong(getUserId()))){
                return R.error("非法操作");
            }
        }
        return R.ok().put("data",address);
    }

}
