package com.fs.company.controller.store;

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.page.TableDataInfo;
import com.fs.common.enums.BusinessType;
import com.fs.common.utils.poi.ExcelUtil;
import com.fs.framework.security.LoginUser;
import com.fs.framework.security.SecurityUtils;
import com.fs.his.domain.FsUser;
import com.fs.his.domain.FsUserAddress;
import com.fs.his.param.FsUserParam;
import com.fs.his.service.IFsUserService;
import com.fs.his.vo.FsUserExportListVO;
import com.fs.his.vo.FsUserVO;
import com.fs.his.vo.UserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

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

/**
 * 用户Controller
 *
 * @author fs
 * @date 2023-06-07
 */
@RestController
@RequestMapping("/store/user")
public class FsUserController extends BaseController
{
    @Autowired
    private IFsUserService fsUserService;
    /**
     * 查询用户列表
     */
    @PreAuthorize("@ss.hasPermi('his:user:list')")
    @GetMapping("/list")
    public TableDataInfo list(FsUserParam fsUser)
    {
        startPage();
        LoginUser loginUser = SecurityUtils.getLoginUser();
        fsUser.setCompanyId(loginUser.getCompany().getCompanyId());
        if(fsUser.getPhoneMk()!=null&&fsUser.getPhone()!=""){
            fsUser.setPhone(encryptPhone(fsUser.getPhoneMk()));
        }

        List<FsUserVO> list = fsUserService.selectFsUserListVOByComponentsUser(fsUser);
        for (FsUserVO fsUserVO : list) {
            fsUserVO.setPhone(decryptAutoPhoneMk(fsUserVO.getPhone()));
        }
        return getDataTable(list);
    }

    @PreAuthorize("@ss.hasPermi('his:user:list')")
    @GetMapping("/userList")
    public TableDataInfo userList(FsUserParam fsUser)
    {
        startPage();
        if(fsUser.getPhoneMk()!=null&&fsUser.getPhone()!=""){
            fsUser.setPhone(encryptPhone(fsUser.getPhoneMk()));
        }
        List<FsUserVO> list = fsUserService.selectFsUserListVOByComponent(fsUser);
        for (FsUserVO fsUserVO : list) {
            fsUserVO.setPhone(decryptAutoPhoneMk(fsUserVO.getPhone()));
        }
        return getDataTable(list);
    }

    @PreAuthorize("@ss.hasPermi('his:user:myList')")
    @GetMapping("/myList")
    public TableDataInfo myList(FsUserParam fsUser)
    {
        startPage();
        LoginUser loginUser = SecurityUtils.getLoginUser();
        fsUser.setCompanyUserId(loginUser.getUser().getUserId());
        if(fsUser.getPhoneMk()!=null&&fsUser.getPhone()!=""){
            fsUser.setPhone(encryptPhone(fsUser.getPhoneMk()));
        }
        List<FsUserVO> list = fsUserService.selectFsUserListVOByComponentsUser(fsUser);
        for (FsUserVO fsUserVO : list) {
            fsUserVO.setPhone(decryptAutoPhoneMk(fsUserVO.getPhone()));
        }
        return getDataTable(list);
    }

    /**
     * 导出用户列表
     */
    @PreAuthorize("@ss.hasPermi('his:user:export')")
    @Log(title = "用户", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(FsUserParam fsUser)
    {
        LoginUser loginUser = SecurityUtils.getLoginUser();

        logger.info("导出用户:"+ loginUser.getUser().getUserId());
        if (fsUserService.isEntityNull(fsUser)){
            return AjaxResult.error("请筛选数据导出");
        }
        fsUser.setCompanyId(loginUser.getCompany().getCompanyId());
        Long count =fsUserService.selectFsUserExportListVOCount(fsUser);
        if (count>30000){
            return AjaxResult.error("导出数据不可超过3w条");
        }
        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, "用户数据");
    }

    @PreAuthorize("@ss.hasPermi('his:user:myExport')")
    @Log(title = "用户", businessType = BusinessType.EXPORT)
    @GetMapping("/myExport")
    public AjaxResult myExport(FsUserParam fsUser)
    {
        LoginUser loginUser = SecurityUtils.getLoginUser();

        logger.info("导出用户:"+ loginUser.getUser().getUserId());
        if (fsUserService.isEntityNull(fsUser)){
            return AjaxResult.error("请筛选数据导出");
        }
        fsUser.setCompanyUserId(loginUser.getUser().getUserId());
        List<FsUserExportListVO> list = fsUserService.selectFsUserExportListVO(fsUser);
        if (list.size()>30000){
            return AjaxResult.error("导出数据不可超过3w条");
        }
        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) {
            fsUserAddress.setPhone(decryptAutoPhoneMk(fsUserAddress.getPhone()));
        }
        return AjaxResult.success();
    }


    /**
     * 新增用户
     */
    @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);
        return getDataTable(list);
    }


}
