package com.fs.company.controller.store;

import com.alibaba.fastjson.JSON;
import com.fs.common.annotation.Log;
import com.fs.common.core.controller.BaseController;
import com.fs.common.core.domain.AjaxResult;
import com.fs.common.core.domain.model.LoginUser;
import com.fs.common.core.page.TableDataInfo;
import com.fs.common.enums.BusinessType;
import com.fs.common.utils.SecurityUtils;
import com.fs.common.utils.poi.ExcelUtil;
import com.fs.his.domain.FsUserAddress;
import com.fs.his.dto.AddressInfoDTO;
import com.fs.his.service.IFsUserAddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 用户地址Controller
 *
 * @author fs
 * @date 2023-12-12
 */
@RestController
@RequestMapping("/store/userAddress")
public class FsUserAddressController extends BaseController
{
    @Autowired
    private IFsUserAddressService fsUserAddressService;

    /**
     * 查询用户地址列表
     */

    @GetMapping("/list")
    public TableDataInfo list(FsUserAddress fsUserAddress)
    {
        startPage();
        List<FsUserAddress> list = fsUserAddressService.selectFsUserAddressList(fsUserAddress);
        for (FsUserAddress userAddress : list) {
            if (userAddress.getPhone()!=null){
                userAddress.setPhone(userAddress.getPhone().replaceAll("(\\d{3})\\d*(\\d{4})", "$1****$2"));
            }
            if (userAddress.getDetail()!=null&&userAddress.getDetail()!=""){
                userAddress.setDetail(userAddress.getDetail().substring(0, userAddress.getDetail().length() / 2) + "********");
            }
        }
        return getDataTable(list);
    }

    /**
     * 导出用户地址列表
     */
    @PreAuthorize("@ss.hasPermi('his:userAddress:export')")
    @Log(title = "用户地址", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(FsUserAddress fsUserAddress)
    {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        logger.info("tc>\n【导出用户地址】："+ loginUser.getUser().getUserId());
        if (fsUserAddressService.isEntityNull(fsUserAddress)){
            return AjaxResult.error("请筛选数据导出");
        }
        Long count =fsUserAddressService.selectFsUserAddressListCount(fsUserAddress);
        if (count>30000){
            return AjaxResult.error("导出数据不可超过3w条");
        }
        List<FsUserAddress> list = fsUserAddressService.selectFsUserAddressList(fsUserAddress);

        ExcelUtil<FsUserAddress> util = new ExcelUtil<FsUserAddress>(FsUserAddress.class);
        return util.exportExcel(list, "用户地址数据");
    }

    /**
     * 获取用户地址详细信息
     */

    @GetMapping(value = "/{addressId}")
    public AjaxResult getInfo(@PathVariable("addressId") Long addressId)
    {
        FsUserAddress fsUserAddress = fsUserAddressService.selectFsUserAddressByAddressId(addressId);
        if (fsUserAddress.getPhone()!=null){
            fsUserAddress.setPhone(fsUserAddress.getPhone().replaceAll("(\\d{3})\\d*(\\d{4})", "$1****$2"));
        }
        if (fsUserAddress.getDetail()!=null){
            fsUserAddress.setDetail(fsUserAddress.getDetail().substring(0, fsUserAddress.getDetail().length() / 2) + "********");

        }
        return AjaxResult.success(fsUserAddress);
    }

    /**
     * 新增用户地址
     */
    @PreAuthorize("@ss.hasPermi('his:userAddress:add')")
    @Log(title = "用户地址", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody FsUserAddress fsUserAddress)
    {
        if(fsUserAddress.getIsDefault()==1){
            //处理默认地址
            fsUserAddressService.clearIsDefalut(fsUserAddress.getUserId());
        }
        return toAjax(fsUserAddressService.insertFsUserAddress(fsUserAddress));
    }



    @GetMapping(value = "/getAddress/{address}")
    public AjaxResult getAddress(@PathVariable("address") String address)
    {
        String kdnAddress = fsUserAddressService.getKdnAddress(address);
        AddressInfoDTO addressInfoDTO = JSON.parseObject(kdnAddress, AddressInfoDTO.class);
        AddressInfoDTO.AddressData data = addressInfoDTO.getData();
        String provinceName = data.getProvinceName();
        if (!provinceName.contains("省") && !provinceName.contains("区")){
            data.setProvinceName(data.getProvinceName()+"市");
            if (data.getExpAreaName().contains("县")){
                data.setCityName("县");
            }else {
                data.setCityName("市辖区");
            }
        }
        return AjaxResult.success(addressInfoDTO);
    }



    /**
     * 修改用户地址
     */
    @PreAuthorize("@ss.hasPermi('his:userAddress:edit')")
    @Log(title = "用户地址", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody FsUserAddress fsUserAddress)
    {
        if (fsUserAddress.getPhone().contains("*")){
            fsUserAddress.setPhone(null);
        }
        if (fsUserAddress.getDetail().contains("*")){
            fsUserAddress.setDetail(null);
        }
        if(fsUserAddress.getIsDefault()==1){
            //处理默认地址
            fsUserAddressService.clearIsDefalut(fsUserAddress.getUserId());
        }
        return toAjax(fsUserAddressService.updateFsUserAddress(fsUserAddress));
    }

    /**
     * 删除用户地址
     */
    @PreAuthorize("@ss.hasPermi('his:userAddress:remove')")
    @Log(title = "用户地址", businessType = BusinessType.DELETE)
	@DeleteMapping("/{addressIds}")
    public AjaxResult remove(@PathVariable Long[] addressIds)
    {
        return toAjax(fsUserAddressService.deleteFsUserAddressByAddressIds(addressIds));
    }
}
