package com.fs.his.controller;

import java.util.List;

import com.fs.common.core.domain.R;
import com.fs.common.utils.ParseUtils;
import com.fs.common.utils.SecurityUtils;
import com.fs.his.domain.FsUserAddress;
import com.fs.his.param.FsUserParam;
import com.fs.his.utils.PhoneUtil;
import com.fs.his.vo.FsUserExportListVO;
import com.fs.his.vo.FsUserVO;
import com.fs.his.vo.UserVo;
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.FsUser;
import com.fs.his.service.IFsUserService;
import com.fs.common.utils.poi.ExcelUtil;
import com.fs.common.core.page.TableDataInfo;

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

/**
 * 用户Controller
 *
 * @author fs
 * @date 2023-06-07
 */
@RestController
@RequestMapping("/his/user")
public class FsUserController extends BaseController
{
    @Autowired
    private IFsUserService fsUserService;

    /**
     * 查询用户列表
     */
    @PreAuthorize("@ss.hasPermi('his:user:list')")
    @GetMapping("/list")
    public TableDataInfo list(FsUserParam fsUser)
    {
        startPage();
        if (fsUser.getPhoneMk()!=null&&fsUser.getPhoneMk()!=""){
            fsUser.setPhone(encryptPhone(fsUser.getPhoneMk()));
        }
        List<FsUserVO> list = fsUserService.selectFsUserListVO(fsUser);
        for (FsUserVO fsUserVO : list) {
            if(fsUserVO.getPhone() != null&&fsUserVO.getPhone()!=""){
                if (fsUserVO.getPhone().length()>11){
                    fsUserVO.setPhone(decryptPhoneMk(fsUserVO.getPhone()));
                }else {
                    fsUserVO.setPhone(fsUserVO.getPhone().replaceAll("(\\d{3})\\d*(\\d{4})", "$1****$2"));
                }

            }

        }
        return getDataTable(list);
    }

    /**
     * 导出用户列表
     */
    @PreAuthorize("@ss.hasPermi('his:user:export')")
    @Log(title = "用户", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(FsUserParam fsUser)
    {
        logger.info("导出用户列表:"+ SecurityUtils.getUserId());
        if (fsUserService.isEntityNull(fsUser)){
            return AjaxResult.error("请筛选数据导出");
        }
        Long count = fsUserService.selectFsUserExportListVOCount(fsUser);
        if (count>10000){
            return AjaxResult.error("导出数据不可超过1w条");
        }
        List<FsUserExportListVO> list = fsUserService.selectFsUserExportListVO(fsUser);
        for (FsUserExportListVO vo : list) {
            if (vo.getMobile()!=null){
                vo.setMobile(vo.getMobile().replaceAll("(\\d{3})\\d*(\\d{4})", "$1****$2"));
            }
        }
        ExcelUtil<FsUserExportListVO> util = new ExcelUtil<FsUserExportListVO>(FsUserExportListVO.class);
        return util.exportExcel(list, "用户数据");
    }

    /**
     * 获取用户详细信息
     */
    @GetMapping(value = "/{userId}")
    public AjaxResult getInfo(@PathVariable("userId") Long userId)
    {
        FsUser fsUser = fsUserService.selectFsUserByUserId(userId);
        fsUser.setPhone(decryptAutoPhoneMk(fsUser.getPhone()));
        return AjaxResult.success(fsUser);
    }



    @GetMapping(value = "/getUserAddr/{userId}")
    public AjaxResult getUserAddr(@PathVariable("userId") Long userId)
    {

        List<FsUserAddress> fsUserAddresses = fsUserService.selectFsUserAddressByUserId(userId);
        for (FsUserAddress fsUserAddress : fsUserAddresses) {
            if (fsUserAddress.getPhone()!=null&&fsUserAddress.getPhone()!=""){
                if (fsUserAddress.getPhone().length()>11){
                    fsUserAddress.setPhone(decryptPhoneMk(fsUserAddress.getPhone()));
                }else {
                    fsUserAddress.setPhone(fsUserAddress.getPhone().replaceAll("(\\d{3})\\d*(\\d{4})", "$1****$2"));
                }
            }
        }

        return AjaxResult.success(fsUserAddresses);
    }


    /**
     * 新增用户
     */
    @PreAuthorize("@ss.hasPermi('his:user:add')")
    @Log(title = "用户", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody FsUser fsUser)
    {
        return toAjax(fsUserService.insertFsUser(fsUser));
    }

    /**
     * 修改用户
     */
    @PreAuthorize("@ss.hasPermi('his:user:edit')")
    @Log(title = "用户", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody FsUser fsUser)
    {
        return toAjax(fsUserService.updateFsUser(fsUser));
    }

    /**
     * 删除用户
     */
    @PreAuthorize("@ss.hasPermi('his:user:remove')")
    @Log(title = "用户", businessType = BusinessType.DELETE)
	@DeleteMapping("/{userIds}")
    public AjaxResult remove(@PathVariable Long[] userIds)
    {
        return toAjax(fsUserService.deleteFsUserByUserIds(userIds));
    }

    /**
     * 查询用户
     */
    @GetMapping("user/list/{name}")
    public TableDataInfo userlist(@PathVariable("name")String name)
    {
        List<UserVo> list = fsUserService.selectUserVOList(name);
        if (list==null||list.size()==0){
            String s = encryptPhone(name);
            list = fsUserService.selectUserVOList(s);
        }
        return getDataTable(list);
    }

    @GetMapping("/listBySearch")
    public R listBySearch(FsUser user)
    {
        List<FsUser> list = fsUserService.selectFsUserList(user);
        return R.ok().put("data",list);
    }


}
