package com.nantian.cloud.jljc.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nantian.cloud.jljc.entity.DischargeEnergyKeyFields;
import com.nantian.cloud.jljc.service.DischargeEnergyKeyFieldsService;
import com.nantian.sc.cloud.common.core.util.R;
import com.nantian.sc.cloud.common.data.datascope.DataScope;
import com.nantian.sc.cloud.common.log.annotation.SysLog;
import com.nantian.sc.cloud.common.security.service.ScUser;
import com.nantian.sc.cloud.common.security.util.SecurityUtils;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * <p>
 * 两高行业及其重点领域 前端控制器
 * </p>
 *
 * @author fyf
 * @since 2022-03-23
 */
@RestController
@CrossOrigin
@AllArgsConstructor
@RequestMapping("/dischargeEnergyKeyFields")
public class DischargeEnergyKeyFieldsController {

    @Autowired
    private DischargeEnergyKeyFieldsService dischargeEnergyKeyFieldsService;

    @GetMapping("/page")
    @SysLog("分页查询两高行业及其重点领域")
    public R getPage(Page page, DischargeEnergyKeyFields dischargeEnergyKeyFields){
        dischargeEnergyKeyFields.setDelFlag(1);
        return R.ok(dischargeEnergyKeyFieldsService.getPage(page, dischargeEnergyKeyFields, new DataScope()));
    }

    @GetMapping("/search")
    @SysLog("查询两高行业及其重点领域")
    public R search(DischargeEnergyKeyFields dischargeEnergyKeyFields){

        return R.ok(dischargeEnergyKeyFieldsService.getSearchList(dischargeEnergyKeyFields));
    }


    @PostMapping("/saveOrUpdate")
    @SysLog("新增/修改两高行业及其重点领域")
    public R saveOrUpdate(@RequestBody DischargeEnergyKeyFields dischargeEnergyKeyFields){

        ScUser u = SecurityUtils.getUser();
        if (dischargeEnergyKeyFields.getId() == null){

            dischargeEnergyKeyFields.setUserId(u.getId());
            dischargeEnergyKeyFields.setDelFlag(1);
            dischargeEnergyKeyFields.setDeptId(u.getDeptId());
            dischargeEnergyKeyFields.setUpdateTime(new Date());
            dischargeEnergyKeyFieldsService.save(dischargeEnergyKeyFields);
        }else {
            dischargeEnergyKeyFields.setUserId(u.getId());
            dischargeEnergyKeyFields.setDeptId(u.getDeptId());
            dischargeEnergyKeyFields.setUpdateTime(new Date());
            dischargeEnergyKeyFieldsService.updateById(dischargeEnergyKeyFields);
        }
        return R.ok();
    }

    @DeleteMapping("/delete/{id}")
    @SysLog("删除两高行业及其重点领域")
    public R delete(@PathVariable Integer id){
        ScUser u = SecurityUtils.getUser();
        DischargeEnergyKeyFields dischargeEnergyKeyFields = dischargeEnergyKeyFieldsService.getById(id);
        dischargeEnergyKeyFields.setDelFlag(4);
        dischargeEnergyKeyFields.setUserId(u.getId());
        dischargeEnergyKeyFields.setDeptId(u.getDeptId());
        dischargeEnergyKeyFields.setUpdateTime(new Date());
        dischargeEnergyKeyFieldsService.updateById(dischargeEnergyKeyFields);
        return R.ok();
    }

}
