package com.fs.company.controller;

import com.fs.common.annotation.Log;
import com.fs.common.core.controller.BaseController;
import com.fs.common.core.domain.R;
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.ServletUtils;
import com.fs.company.domain.CompanyTrafficRecord;
import com.fs.company.param.CompanyTrafficRecordChargeParam;
import com.fs.company.param.CompanyTrafficRecordQueryParam;
import com.fs.company.service.ICompanyTrafficRecordService;
import com.fs.framework.web.service.TokenService;
import com.fs.hisStore.config.MedicalMallConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.PostConstruct;
import java.util.List;

@RestController
@RequestMapping("/company/traffic")
public class CompanyTrafficController extends BaseController {

    @Autowired
    private ICompanyTrafficRecordService companyTrafficRecordService;

    @Autowired
    private TokenService tokenService;

    @Autowired
    private MedicalMallConfig medicalMallConfig;

    /**
     * 每天扣除流量余额
     * */
    @Scheduled(cron = "0 1 0 * * ?")
    public void refreshTraffic(){
        if(medicalMallConfig.isStatics())
            companyTrafficRecordService.refreshTraffic();
    }

    /**
     * 重启时重新计算并更新公司缓存
     * */
    @PostConstruct
    public void init() {
        if(medicalMallConfig.isStatics())
            companyTrafficRecordService.init();
    }

    /** 充值流量 */
    @PreAuthorize("@ss.hasPermi('company:traffic:charge')")
    @Log(title = "公司流量充值", businessType = BusinessType.INSERT)
    @PostMapping(value = "/rechargeTraffic")
    public R rechargeTraffic(@RequestBody CompanyTrafficRecordChargeParam companyTrafficRecord) {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        companyTrafficRecord.setUserId(loginUser.getUser().getUserId());
        companyTrafficRecord.setUserName(loginUser.getUser().getUserName());
        companyTrafficRecordService.recharge(companyTrafficRecord);
        return R.ok();
    }

    /** 流量充值记录查询 */
    @PreAuthorize("@ss.hasPermi('company:traffic:list')")
    @GetMapping(value = "/list")
    public TableDataInfo list(CompanyTrafficRecordQueryParam param) {
        startPage();
        List<CompanyTrafficRecord> list = companyTrafficRecordService.selectList(param);
        return getDataTable(list);
    }

    /** 流量充值详情 */
    @PreAuthorize("@ss.hasPermi('company:traffic:detail')")
    @GetMapping(value = "/detail/{logId}")
    public R detail(@PathVariable("logId") Long logId) {
        CompanyTrafficRecord record = companyTrafficRecordService.selectById(logId);
        return R.ok().put("data", record);
    }

    /** 流量换算*/
    @GetMapping(value = "/trafficConversion")
    public R trafficConversion(@RequestParam("traffic") Long amount) {
        Long trafficKB = companyTrafficRecordService.trafficConversion(amount);
        return R.ok().put("data", trafficKB);
    }

    /** 刷新单价*/
    @GetMapping(value = "/refreshPrice")
    public R refreshPrice() {
        companyTrafficRecordService.refreshPrice();
        return R.ok();
    }

    /** 流量统计*/
    @GetMapping(value = "/trafficStatistics")
    public R trafficStatistics(CompanyTrafficRecord record) {
        return R.ok().put("tatol","").put("dept","").put("company","");
    }
}
