package com.xhpc.activity.controller;


import com.xhpc.activity.domain.XhpcActivityInternetDomain;
import com.xhpc.activity.service.XhpcActivityInternetService;
import com.xhpc.common.core.domain.R;
import com.xhpc.common.core.web.controller.BaseController;
import com.xhpc.common.core.web.page.TableDataInfo;
import com.xhpc.common.log.annotation.Log;
import com.xhpc.common.log.enums.BusinessType;
import com.xhpc.common.util.LogUserUtils;
import com.xhpc.system.api.model.LoginUser;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/activity/internet")
public class XhpcActivityInternetController extends BaseController {

    @Resource
    XhpcActivityInternetService internetService;

    @Resource
    LogUserUtils logUserUtils;


    @GetMapping("/getPage")
    public TableDataInfo getPage(HttpServletRequest request,
                                 String status){
        LoginUser loginUser = logUserUtils.getLogUser(request);
        startPage();
        Map<String, Object> params = new HashMap<>();
        params.put("tenantId", loginUser.getTenantId());
        params.put("status", status);
        return getDataTable(internetService.getPage(params));
    }


    @GetMapping("/getNameList")
    public R getNameList(HttpServletRequest request){
        LoginUser loginUser = logUserUtils.getLogUser(request);

        Map<String, Object> params = new HashMap<>();
        params.put("tenantId", loginUser.getTenantId());
        return R.ok(internetService.getNameList(params));
    }


    @GetMapping("/detail")
    public R getDetail(@RequestParam("activityId")Integer activityId){
        return R.ok(internetService.getDomainByPk(activityId));
    }


    @Log(title = "活动管理-新增流量方活动", businessType = BusinessType.INSERT)
    @PostMapping("/")
    public R insertDomain(HttpServletRequest request, @RequestBody XhpcActivityInternetDomain domain){

        LoginUser loginUser = logUserUtils.getLogUser(request);
        domain.setCreateBy(loginUser.getUserid().toString());
        domain.setUpdateBy(loginUser.getUserid().toString());
        domain.setTenantId(loginUser.getTenantId());
        domain.setStatus(Short.valueOf("1"));
        return internetService.insertDomain(domain);
    }


    @Log(title = "活动管理-更新流量方活动模板", businessType = BusinessType.UPDATE)
    @PutMapping("/detail")
    public R updateDomain(HttpServletRequest request, @RequestBody XhpcActivityInternetDomain domain){
        LoginUser loginUser = logUserUtils.getLogUser(request);
        domain.setUpdateBy(loginUser.getUserid().toString());
        return R.ok(internetService.updateDomain(domain));
    }


    @Log(title = "活动管理-更新流量方活动状态", businessType = BusinessType.UPDATE)
    @PutMapping("/status")
    public R updateDomainStatus(HttpServletRequest request, @RequestBody XhpcActivityInternetDomain domain){
        LoginUser loginUser = logUserUtils.getLogUser(request);
        domain.setUpdateBy(loginUser.getUserid().toString());
        return R.ok(internetService.updateDomainStatus(domain));
    }


    @Log(title = "活动管理-审核流量方活动", businessType = BusinessType.UPDATE)
    @PutMapping("/check")
    public R checkDomain(HttpServletRequest request, @RequestBody XhpcActivityInternetDomain domain){
        LoginUser loginUser = logUserUtils.getLogUser(request);
        domain.setCheckBy(loginUser.getUserid().toString());
        return R.ok(internetService.checkDomain(domain));
    }


    @Log(title = "活动管理-删除流量方活动", businessType = BusinessType.DELETE)
    @DeleteMapping("/{activityId}")
    public R deleteDomainByPk(@PathVariable("activityId")Integer activityId){
        return R.ok(internetService.deleteByPk(activityId));
    }

}
