package com.fs.company.controller;

import com.fs.common.core.controller.BaseController;
import com.fs.common.core.domain.R;
import com.fs.common.core.page.TableDataInfo;
import com.fs.common.utils.ServletUtils;
import com.fs.company.domain.Company;
import com.fs.company.domain.CompanySms;
import com.fs.company.domain.CompanyVoice;
import com.fs.company.param.CompanyParam;
import com.fs.company.service.ICompanyService;
import com.fs.company.service.ICompanySmsService;
import com.fs.company.service.ICompanyVoiceService;
import com.fs.company.vo.CompanyNameVO;
import com.fs.core.security.LoginUser;
import com.fs.core.web.service.TokenService;
import com.fs.his.vo.OptionsVO;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 企业Controller
 *
 * @author fs
 * @date 2021-10-04
 */
@RestController
@RequestMapping("/company/company")
public class CompanyController extends BaseController
{
    @Autowired
    private ICompanyService companyService;
    @Autowired
    private ICompanyVoiceService companyVoiceService;
    @Autowired
    private ICompanySmsService companySmsService;

    @Autowired
    private TokenService tokenService;


    /**
     * 查询企业列表
     */
    @GetMapping("/list")
    public TableDataInfo list(CompanyParam param)
    {
        startPage();
        List<CompanyNameVO> list = companyService.selectCompanyNameVOList(param);
        return getDataTable(list);
    }


    @GetMapping(value = "/getCompanyInfo")
    public R getCompanyInfo()
    {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        Company company=companyService.selectCompanyById(loginUser.getCompany().getCompanyId());
        return R.ok().put("data",company);
    }
    @GetMapping(value = "/getCompanyVoice")
    public R getCompanyVoice()
    {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        CompanyVoice voice=companyVoiceService.selectCompanyVoiceByCompanyId(loginUser.getCompany().getCompanyId());
        if(voice==null){
            voice=new CompanyVoice();
            voice.setTimes(0l);
            voice.setTotalTimes(0l);
            voice.setCompanyId(loginUser.getCompany().getCompanyId());
            companyVoiceService.insertCompanyVoice(voice);
        }
        return R.ok().put("data",voice);
    }

    @GetMapping(value = "/getCompanySms")
    public R getCompanySms()
    {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        CompanySms sms=companySmsService.selectCompanySmsByCompanyId(loginUser.getCompany().getCompanyId());
        if(sms==null){
            sms=new CompanySms();
            sms.setRemainSmsCount(0l);
            sms.setTotalSmsCount(0l);
            sms.setCompanyId(loginUser.getCompany().getCompanyId());
            companySmsService.insertCompanySms(sms);
        }
        return R.ok().put("data",sms);
    }

    @GetMapping("/getCompanyList")
    public R getCompanyList()
    {
        Company map=new Company();
        map.setIsDel(0);
        List<Company> list = companyService.selectCompanyList(map);
        return R.ok().put("data",list);
    }

    @GetMapping("/allList")
    public TableDataInfo getHospital()
    {
        List<OptionsVO> list = companyService.selectAllCompanyList();
        return getDataTable(list);
    }

    @ApiOperation("配置销售审核")
    @PostMapping(value = "/configUserCheck")
    public R configUserCheck(boolean userIsDefaultBlack) {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        companyService.configUserCheck(loginUser.getCompany().getCompanyId(), userIsDefaultBlack ? 1 : 0);
        return R.ok();
    }

}
