package com.fs.company.controller.company;

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.ServletUtils;
import com.fs.common.utils.poi.ExcelUtil;
import com.fs.company.domain.CompanySmsLogs;
import com.fs.company.param.CompanySmsLogsListQueryParam;
import com.fs.company.service.ICompanySmsLogsService;
import com.fs.company.vo.CompanySmsLogsListQueryVO;
import com.fs.framework.security.LoginUser;
import com.fs.framework.service.TokenService;
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-01-09
 */
@RestController
@RequestMapping("/company/companySmsLogs")
public class CompanySmsLogsController extends BaseController
{
    @Autowired
    private ICompanySmsLogsService companySmsLogsService;
    @Autowired
    private TokenService tokenService;
    /**
     * 查询短信发送记录列表
     */
    @GetMapping("/list")
    public TableDataInfo list(CompanySmsLogsListQueryParam param)
    {
        startPage();


        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        param.setCompanyId(loginUser.getCompany().getCompanyId());
        List<CompanySmsLogsListQueryVO> list = companySmsLogsService.selectCompanySmsLogsListQuery(param);
        if (list != null) {
            for (CompanySmsLogsListQueryVO vo : list) {
                if(vo.getPhone()!=null){
                    vo.setPhone(vo.getPhone().replaceAll("(\\d{3})\\d*(\\d{4})", "$1****$2"));
                }

            }
        }
        return getDataTable(list);
    }

    /**
     * 导出短信发送记录列表
     */
    @PreAuthorize("@ss.hasPermi('company:companySmsLogs:export')")
    @Log(title = "短信发送记录", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(CompanySmsLogsListQueryParam param)
    {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        param.setCompanyId(loginUser.getCompany().getCompanyId());
        List<CompanySmsLogsListQueryVO> list = companySmsLogsService.selectCompanySmsLogsListQuery(param);
        if (list != null) {
            for (CompanySmsLogsListQueryVO vo : list) {
                if(vo.getPhone()!=null){
                    vo.setPhone(vo.getPhone().replaceAll("(\\d{3})\\d*(\\d{4})", "$1****$2"));
                }

            }
        }
        ExcelUtil<CompanySmsLogsListQueryVO> util = new ExcelUtil<CompanySmsLogsListQueryVO>(CompanySmsLogsListQueryVO.class);
        return util.exportExcel(list, "companySmsLogs");
    }

    /**
     * 获取短信发送记录详细信息
     */
    @PreAuthorize("@ss.hasPermi('company:companySmsLogs:query')")
    @GetMapping(value = "/{logsId}")
    public AjaxResult getInfo(@PathVariable("logsId") Long logsId)
    {
        return AjaxResult.success(companySmsLogsService.selectCompanySmsLogsById(logsId));
    }

    /**
     * 新增短信发送记录
     */
    @PreAuthorize("@ss.hasPermi('company:companySmsLogs:add')")
    @Log(title = "短信发送记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CompanySmsLogs companySmsLogs)
    {
        return toAjax(companySmsLogsService.insertCompanySmsLogs(companySmsLogs));
    }

    /**
     * 修改短信发送记录
     */
    @PreAuthorize("@ss.hasPermi('company:companySmsLogs:edit')")
    @Log(title = "短信发送记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CompanySmsLogs companySmsLogs)
    {
        return toAjax(companySmsLogsService.updateCompanySmsLogs(companySmsLogs));
    }

    /**
     * 删除短信发送记录
     */
    @PreAuthorize("@ss.hasPermi('company:companySmsLogs:remove')")
    @Log(title = "短信发送记录", businessType = BusinessType.DELETE)
	@DeleteMapping("/{logsIds}")
    public AjaxResult remove(@PathVariable Long[] logsIds)
    {
        return toAjax(companySmsLogsService.deleteCompanySmsLogsByIds(logsIds));
    }
}
