package com.ruoyi.web.controller.dxp.customer;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.dxp.domain.Application;
import com.ruoyi.dxp.service.IApplicationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 企业应用
 */
@RestController
@RequestMapping("/customer/application")
public class ApplicationCustomerController extends BaseController {

    @Autowired
    private IApplicationService applicationService;

    /**
     * 我的应用
     *
     * @param application
     * @return
     */
    @GetMapping("/list")
    public AjaxResult list(Application application) {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        application.setUserId(loginUser.getUserId());
        List<Application> list = applicationService.selectApplicationList(application);
        return success(list);
    }

    /**
     * 应用详情
     *
     * @param id
     * @return
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        Application application = applicationService.selectApplicationById(id);
        if (application != null && !application.getUserId().equals(loginUser.getUserId())) {
            return success();
        }

        return success(application);
    }

    /**
     * 添加应用
     * @param dto
     * @return
     */
    @Log(title = "应用", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Application dto) {
        return applicationService.insertApplication(dto);
    }

    /**
     * 编辑应用
     * @param dto
     * @return
     */
    @Log(title = "应用", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Application dto) {
        return applicationService.updateApplication(dto);
    }

    /**
     * 删除应用
     * @param ids
     * @return
     */
    @Log(title = "应用", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long ids)
    {
        return applicationService.deleteApplicationById(ids);
    }
}
