package com.fs.his.controller;

import java.util.List;

import com.alibaba.fastjson.JSON;
import com.fs.common.core.domain.R;
import com.fs.common.utils.ParseUtils;
import com.fs.common.utils.SecurityUtils;
import com.fs.his.dto.AddressInfoDTO;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.fs.common.annotation.Log;
import com.fs.common.core.controller.BaseController;
import com.fs.common.core.domain.AjaxResult;
import com.fs.common.enums.BusinessType;
import com.fs.his.domain.FsUserAddress;
import com.fs.his.service.IFsUserAddressService;
import com.fs.common.utils.poi.ExcelUtil;
import com.fs.common.core.page.TableDataInfo;

import static com.fs.his.utils.PhoneUtil.decryptAutoPhoneMk;

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

    /**
     * 查询用户地址列表
     */
    @PreAuthorize("@ss.hasPermi('his:userAddress:list')")
    @GetMapping("/list")
    public TableDataInfo list(FsUserAddress fsUserAddress)
    {
        startPage();
        List<FsUserAddress> list = fsUserAddressService.selectFsUserAddressList(fsUserAddress);
        for (FsUserAddress userAddress : list) {
            userAddress.setPhone(decryptAutoPhoneMk(userAddress.getPhone()));
            userAddress.setDetail(ParseUtils.parseAddress(userAddress.getDetail()));
        }
        return getDataTable(list);
    }

    /**
     * 导出用户地址列表
     */
    @PreAuthorize("@ss.hasPermi('his:userAddress:export')")
    @Log(title = "用户地址", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(FsUserAddress fsUserAddress)
    {
        logger.info("tc>\n【导出用户地址】：{}", SecurityUtils.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, "用户地址数据");
    }

    /**
     * 获取用户地址详细信息
     */
    @PreAuthorize("@ss.hasPermi('his:userAddress:query')")
    @GetMapping(value = "/{addressId}")
    public AjaxResult getInfo(@PathVariable("addressId") Long addressId)
    {
        FsUserAddress fsUserAddress = fsUserAddressService.selectFsUserAddressByAddressId(addressId);
        fsUserAddress.setPhone(ParseUtils.parsePhone(fsUserAddress.getPhone()));
        fsUserAddress.setDetail(ParseUtils.parseAddress(fsUserAddress.getDetail()));
        return AjaxResult.success(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: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));
    }

    /**
     * 修改用户地址
     */
    @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));
    }
}
