package com.fs.app.controller;


import cn.hutool.core.bean.BeanUtil;
import com.fs.app.annotation.Login;
import com.fs.app.param.FsAddressParseParam;
import com.fs.app.param.FsUserAddressAddEditParam;
import com.fs.common.annotation.RepeatSubmit;
import com.fs.common.core.domain.R;
import com.fs.his.domain.FsCity;
import com.fs.his.domain.FsUserAddress;
import com.fs.his.service.IFsCityService;
import com.fs.his.service.IFsExpressService;
import com.fs.his.service.IFsUserAddressService;
import com.fs.his.service.IFsUserIntegralLogsService;
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 javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;

import static com.fs.his.utils.PhoneUtil.decryptPhone;
import static com.fs.his.utils.PhoneUtil.encryptPhone;


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


    @Autowired
    private IFsUserAddressService addressService;

    @Autowired
    private IFsCityService cityService;
    @Autowired
    private IFsExpressService expressService;
    @Autowired
    private IFsUserIntegralLogsService fsUserIntegralLogsService;




    @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);
            for (FsUserAddress fsUserAddress : list) {

                if (fsUserAddress.getPhone()!=null&&fsUserAddress.getPhone().length()>11&&!fsUserAddress.getPhone().matches("\\d+")){
                    fsUserAddress.setPhone(decryptPhone(fsUserAddress.getPhone()));
                }
            }
            return R.ok().put("data",list);
        } catch (Exception e){
            return R.error("操作异常");
        }
    }
    @Login
    @ApiOperation("获取地址信息")
    @GetMapping("/getAddressById")
    public R getAddressById(@RequestParam("addressId")Long addressId, HttpServletRequest request){
        FsUserAddress address=addressService.selectFsUserAddressByAddressId(addressId);
        if (address.getPhone()!=null&&address.getPhone().length()>11&&!address.getPhone().matches("\\d+")){
            address.setPhone(decryptPhone(address.getPhone()));
        }
        if(!address.getUserId().equals(Long.parseLong(getUserId()))){
            return R.error("非法操作");
        }
        return R.ok().put("data",address);
    }
    @Login
    @RepeatSubmit
    @ApiOperation("添加地址")
    @PostMapping("/addAddress")
    public R addAddress(@Valid @RequestBody FsUserAddressAddEditParam address, HttpServletRequest request){
        //处理地址数量 最大20个
        Integer count=addressService.selectFsUserAddressCountsByUserId(Long.parseLong(getUserId()));
        if(count>10){
            return R.error("最多可创建10个地址");
        }
        if(address.getIsDefault()==1){
            //处理默认地址
            addressService.clearIsDefalut(Long.parseLong(getUserId()));
        }
        FsUserAddress userAddress=new FsUserAddress();
        BeanUtil.copyProperties(address, userAddress);
        address.setPhone(address.getPhone().trim());
        userAddress.setUserId(Long.parseLong(getUserId()));
        addressService.insertFsUserAddress(userAddress);
        fsUserIntegralLogsService.addUserAddressIntegral(Long.parseLong(getUserId()),userAddress.getAddressId());
        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.deleteFsUserAddressByAddressId(address.getAddressId());
        return R.ok("操作成功");
    }

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

    @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(decryptPhone(address.getPhone()));
        }
        return R.ok().put("data",address);
    }

}
