package com.supervision.web.controller.grassrootsregistration;

import java.util.HashMap;
import java.util.List;

import com.supervision.common.core.domain.entity.SysRole;
import com.supervision.common.core.domain.model.LoginUser;
import com.supervision.framework.web.domain.server.Sys;
import com.supervision.system.domain.BdglCookcategory;
import com.supervision.system.mapper.SysUserMapper;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.supervision.common.annotation.Log;
import com.supervision.common.core.controller.BaseController;
import com.supervision.common.core.domain.AjaxResult;
import com.supervision.common.enums.BusinessType;
import com.supervision.grassrootsregistration.domain.BdglCookbook;
import com.supervision.grassrootsregistration.service.IBdglCookbookService;
import com.supervision.common.utils.poi.ExcelUtil;
import com.supervision.common.core.page.TableDataInfo;

/**
 * 一周食谱Controller
 * 
 * @author supervision
 * @date 2022-02-26
 */
@RestController
@RequestMapping("/grassrootsregistration/bdglcookbook")
public class BdglCookbookController extends BaseController
{
    @Autowired
    private IBdglCookbookService bdglCookbookService;
    @Autowired
    private SysUserMapper userMapper;

    /**
     * 查询一周食谱列表
     */
    @PreAuthorize("@ss.hasPermi('grassrootsregistration:bdglcookbook:list')")
    @GetMapping("/list")
    public TableDataInfo list(BdglCookbook bdglCookbook)
    {
        LoginUser loginUser = getLoginUser();
        List<SysRole> sysRoles = userMapper.selectRole(loginUser.getUserId());
        String role = CommonsController.getRole(sysRoles);
        //判断当前角色是否是最高权限   不是返回当前部门数据
        if(!"user_admin".equals(role)){
            bdglCookbook.setUnitId(Integer.parseInt(loginUser.getDeptId().toString()));
        }
        startPage();
        List<BdglCookbook> list = bdglCookbookService.selectBdglCookbookList(bdglCookbook);
        return getDataTable(list);
    }

    /**
     * 导出一周食谱列表
     */
    @PreAuthorize("@ss.hasPermi('grassrootsregistration:bdglcookbook:export')")
    @Log(title = "一周食谱", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(BdglCookbook bdglCookbook)
    {
        List<BdglCookbook> list = bdglCookbookService.selectBdglCookbookList(bdglCookbook);
        ExcelUtil<BdglCookbook> util = new ExcelUtil<BdglCookbook>(BdglCookbook.class);
        return util.exportExcel(list, "一周食谱数据");
    }

    /**
     * 获取一周食谱详细信息
     */
    @PreAuthorize("@ss.hasPermi('grassrootsregistration:bdglcookbook:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Integer id)
    {
        return AjaxResult.success(bdglCookbookService.selectBdglCookbookById(id));
    }

    /**
     * 新增一周食谱
     */
    @PreAuthorize("@ss.hasPermi('grassrootsregistration:bdglcookbook:add')")
    @Log(title = "一周食谱", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BdglCookbook bdglCookbook)
    {
        return toAjax(bdglCookbookService.insertBdglCookbook(bdglCookbook));
    }

    /**
     * 修改一周食谱
     */
    @PreAuthorize("@ss.hasPermi('grassrootsregistration:bdglcookbook:edit')")
    @Log(title = "一周食谱", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BdglCookbook bdglCookbook)
    {
        return toAjax(bdglCookbookService.updateBdglCookbook(bdglCookbook));
    }

    /**
     * 删除一周食谱
     */
    @PreAuthorize("@ss.hasPermi('grassrootsregistration:bdglcookbook:remove')")
    @Log(title = "一周食谱", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Integer[] ids)
    {
        return toAjax(bdglCookbookService.deleteBdglCookbookByIds(ids));
    }

    /**获取食品*/
    @GetMapping("/getfood")
    public AjaxResult getFood(){
        HashMap<String, Object> cookcategorys=bdglCookbookService.getFood();
        return AjaxResult.success(cookcategorys);
    }
}
