package com.willalp.web.controller.app;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.willalp.app.domain.HsAppHome;
import com.willalp.app.domain.HsAppHomeData;
import com.willalp.app.service.IHsAppHomeService;
import com.willalp.common.annotation.Log;
import com.willalp.common.core.controller.BaseController;
import com.willalp.common.core.domain.AjaxResult;
import com.willalp.common.core.domain.vo.SysDeptVo;
import com.willalp.common.core.page.TableDataInfo;
import com.willalp.common.enums.BusinessType;
import com.willalp.common.utils.DateUtils;
import com.willalp.common.utils.SecurityUtils;
import com.willalp.common.utils.poi.ExcelUtil;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

/**
 * app首页Controller
 *
 * @author willalp
 * @date 2022-01-11
 */
@RestController
@RequestMapping("/app/apphome")
public class
HsAppHomeController extends BaseController {
    @Resource
    private IHsAppHomeService hsAppHomeService;

    /**
     * 查询app首页列表
     */
    @PreAuthorize("@ss.hasPermi('app:apphome:list')")
    @GetMapping("/list")
    public TableDataInfo list(HsAppHome hsAppHome) {
        startPage();
        List<HsAppHome> list = hsAppHomeService.selectHsAppHomeList(hsAppHome, getUsername());
        return getDataTable(list);
    }


    @GetMapping("/orgList")
    public AjaxResult orgList() {
        List<SysDeptVo> sysDeptVos = hsAppHomeService.selectSysDeptList();
        return AjaxResult.success(sysDeptVos);
    }

    /**
     * 导出app首页列表
     */
    @PreAuthorize("@ss.hasPermi('app:apphome:export')")
    @Log(title = "app首页", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(HsAppHome hsAppHome) {
        List<HsAppHome> list = hsAppHomeService.selectHsAppHomeList(hsAppHome, getUsername());
        ExcelUtil<HsAppHome> util = new ExcelUtil<HsAppHome>(HsAppHome.class);
        return util.exportExcel(list, "app首页数据");
    }

    /**
     * 获取app首页详细信息
     */
    @PreAuthorize("@ss.hasPermi('app:apphome:query')")
    @GetMapping(value = "/{appHomeId}")
    public AjaxResult getInfo(@PathVariable("appHomeId") Long appHomeId) {
        return AjaxResult.success(hsAppHomeService.getById(appHomeId));
    }

    /**
     * 新增app首页
     */
    @PreAuthorize("@ss.hasPermi('app:apphome:add')")
    @Log(title = "app首页", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody HsAppHome hsAppHome) {
        hsAppHome.setCreateBy(getUsername());
        hsAppHome.setCreateTime(DateUtils.getNowDate());
        //新增对应方案数据父级
        HsAppHomeData appHomeData = new HsAppHomeData();
        appHomeData.setHomeParentId("0");
        appHomeData.setHomeGroupType(0);
        appHomeData.setCreateBy(getUsername());
        return toAjax(hsAppHomeService.saveHomeProject(hsAppHome, appHomeData));
    }

    /**
     * 修改app首页
     */
    @PreAuthorize("@ss.hasPermi('app:apphome:edit')")
    @Log(title = "app首页", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody HsAppHome hsAppHome) {
        if (1 == hsAppHome.getIsIssue()) {
            HsAppHome appHome = hsAppHomeService.getOne(new QueryWrapper<HsAppHome>()
                    .eq("is_issue", 1)
                    .eq("org_code", hsAppHome.getOrgCode()));
            if (ObjectUtil.isNotNull(appHome)) {
                appHome.setIsIssue(0);
                if (!hsAppHomeService.updateById(appHome)) {
                    return AjaxResult.error("发布失败");
                }
            }
        }
        hsAppHome.setUpdateBy(SecurityUtils.getUsername());
        hsAppHome.setUpdateTime(DateUtils.getNowDate());
        return toAjax(hsAppHomeService.updateById(hsAppHome));
    }

    /**
     * 删除app首页
     */
    @PreAuthorize("@ss.hasPermi('app:apphome:remove')")
    @Log(title = "app首页", businessType = BusinessType.DELETE)
    @DeleteMapping("/{appHomeIds}")
    public AjaxResult remove(@PathVariable Long[] appHomeIds) {
        return toAjax(hsAppHomeService.removeByIds(Arrays.asList(appHomeIds)));
    }
}
