package io.renren.modules.admin.controller;

import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;
import io.renren.modules.admin.entity.IndustryEntity;
import io.renren.modules.admin.service.IndustryService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
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.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;



/**
 * 
 *
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2020-02-21 14:56:20
 */
@RestController
@RequestMapping("admin/industry")
public class IndustryController {
    @Autowired
    private IndustryService industryService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("admin:industry:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = industryService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("admin:industry:info")
    public R info(@PathVariable("id") Long id){
		IndustryEntity industry = industryService.getById(id);

        return R.ok().put("industry", industry);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("admin:industry:save")
    public R save(@RequestBody IndustryEntity industry){
        //创建时间
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-mm-dd hh:mm:ss");
        java.util.Date time = null;
        try {
            time= sdf.parse(sdf.format(new Date()));

        } catch (ParseException e) {
            e.printStackTrace();
        }
        industry.setCreateTime(time);
        int i = industryService.selectIsName(industry.getName());
        if(i==0) {
            industryService.save(industry);
            return R.ok();
        }else{
            return  R.error("该行业名称已经存在");
        }

    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("admin:industry:update")
    public R update(@RequestBody IndustryEntity industry){
		industryService.updateById(industry);

        return R.ok();
    }

    /**
     * 逻辑删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("admin:industry:delete")
    public R delete(@RequestBody Long[] ids){
        industryService.delete(ids);
        return R.ok();
    }

    /**
     * findAllByName 根据行业名称模糊搜索
     */
    @RequestMapping("/findAllByName")
    @RequiresPermissions("admin:industry:findAllByName")
    public List<IndustryEntity> findAllByName(@RequestParam Map<String,Object> params){
        String name = (String) params.get("name");
        List<IndustryEntity> allByName = industryService.findAllByName(name);
        return  allByName;
    }

    /**
     * selectIsName 查询行业名称是否存在
     */
    @RequestMapping("/selectIsName")
    @RequiresPermissions("admin:industry:selectIsName")
    public int selectIsName(@RequestParam Map<String,Object> params){
        String name = (String) params.get("name");
        int i = industryService.selectIsName(name);
        return i;
    }
}
