package io.renren.modules.admin.controller;

import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
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 io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;
import io.renren.common.validator.ValidatorUtils;
import io.renren.common.validator.group.AddGroup;
import io.renren.common.validator.group.UpdateGroup;
import io.renren.modules.admin.entity.DataBaseEntity;
import io.renren.modules.admin.entity.DataCorrosionEntity;
import io.renren.modules.admin.entity.DataCraftEntity;
import io.renren.modules.admin.entity.DataMechanicsEntity;
import io.renren.modules.admin.entity.DataPhysicalEntity;
import io.renren.modules.admin.entity.DataStructureEntity;
import io.renren.modules.admin.form.DataForm;
import io.renren.modules.admin.service.DataBaseService;
import io.renren.modules.admin.service.DataCorrosionService;
import io.renren.modules.admin.service.DataCraftService;
import io.renren.modules.admin.service.DataMechanicsService;
import io.renren.modules.admin.service.DataPhysicalService;
import io.renren.modules.admin.service.DataStructureService;

/**
 * 
 *
 * @author huwhois
 * @email huwhois@gmail.com
 * @date 2020-07-22 15:28:24
 */
@RestController
@RequestMapping("/admin/data")
public class DataController {
    @Autowired
    private DataBaseService dataBaseService;
    @Autowired
    private DataPhysicalService dataPhysicalService;
    @Autowired
    private DataMechanicsService dataMechanicsService;
    @Autowired
    private DataCraftService dataCraftService;
    @Autowired
    private DataStructureService dataStructureService;
    @Autowired
    private DataCorrosionService dataCorrosionService;

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

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

    /**
     * 信息
     */
    @GetMapping("/info/{id}")
    @RequiresPermissions("admin:data:info")
    public R info(@PathVariable("id") Long id){
        DataBaseEntity dataBase = dataBaseService.getById(id);
        DataPhysicalEntity dataPhysical = dataPhysicalService.getById(id);
        DataMechanicsEntity dataMechanics = dataMechanicsService.getById(id);
        DataCraftEntity dataCraft = dataCraftService.getById(id);
        DataStructureEntity dataStructure = dataStructureService.getById(id);
        DataCorrosionEntity dataCorrosion = dataCorrosionService.getById(id);
        Map<String, Object> data = new HashMap<>();
        data.put("dataBase", dataBase);
        data.put("dataPhysical", dataPhysical);
        data.put("dataMechanics", dataMechanics);
        data.put("dataCraft", dataCraft);
        data.put("dataStructure", dataStructure);
        data.put("dataCorrosion", dataCorrosion);

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

    /**
     * 保存
     */
    @PostMapping("/save")
    @RequiresPermissions("admin:data:save")
    public R save(@RequestBody DataForm data){
        DataBaseEntity dataBase = (DataBaseEntity) data.getDataBase();
        DataPhysicalEntity dataPhysicalEntity = (DataPhysicalEntity) data.getDataPhysical();
        DataMechanicsEntity dataMechanicsEntit = (DataMechanicsEntity) data.getDataMechanics();
        DataCraftEntity dataCraftEntit = (DataCraftEntity) data.getDataCraft();
        DataStructureEntity dataStructureEntit = (DataStructureEntity) data.getDataStructure();
        DataCorrosionEntity dataCorrosionEntit = (DataCorrosionEntity) data.getDataCorrosion();

        ValidatorUtils.validateEntity(dataBase, AddGroup.class);

        dataBase.setCreateTime(new Date());
        dataBaseService.save(dataBase);
        Long dId = dataBase.getId();

        dataPhysicalEntity.setDId(dId);
        dataPhysicalEntity.setCode(dataBase.getCode());
        dataPhysicalService.save(dataPhysicalEntity);

        dataMechanicsEntit.setDId(dId);
        dataMechanicsEntit.setCode(dataBase.getCode());
        dataMechanicsService.save(dataMechanicsEntit);
        
        dataCraftEntit.setDId(dId);
        dataCraftEntit.setCode(dataBase.getCode());
        dataCraftService.save(dataCraftEntit);
        
        dataStructureEntit.setDId(dId);
        dataStructureEntit.setCode(dataBase.getCode());
        dataStructureService.save(dataStructureEntit);
        
        dataCorrosionEntit.setDId(dId);
        dataCorrosionEntit.setCode(dataBase.getCode());
        dataCorrosionService.save(dataCorrosionEntit);
        
        return R.ok();
    }

    /**
     * 修改
     */
    @PostMapping("/update")
    @RequiresPermissions("admin:data:update")
    public R update(@RequestBody DataForm data){
        DataBaseEntity dataBase = (DataBaseEntity) data.getDataBase();
        DataPhysicalEntity dataPhysicalEntity = (DataPhysicalEntity) data.getDataPhysical();
        DataMechanicsEntity dataMechanicsEntit = (DataMechanicsEntity) data.getDataMechanics();
        DataCraftEntity dataCraftEntit = (DataCraftEntity) data.getDataCraft();
        DataStructureEntity dataStructureEntit = (DataStructureEntity) data.getDataStructure();
        DataCorrosionEntity dataCorrosionEntit = (DataCorrosionEntity) data.getDataCorrosion();

        ValidatorUtils.validateEntity(dataBase, UpdateGroup.class);
        dataBaseService.updateById(dataBase);

        Long dId = dataBase.getId();
        dataPhysicalEntity.setDId(dId);
        dataPhysicalEntity.setCode(dataBase.getCode());
        dataPhysicalService.updateById(dataPhysicalEntity);

        dataMechanicsEntit.setDId(dId);
        dataMechanicsEntit.setCode(dataBase.getCode());
        dataMechanicsService.updateById(dataMechanicsEntit);
        
        dataCraftEntit.setDId(dId);
        dataCraftEntit.setCode(dataBase.getCode());
        dataCraftService.updateById(dataCraftEntit);
        
        dataStructureEntit.setDId(dId);
        dataStructureEntit.setCode(dataBase.getCode());
        dataStructureService.updateById(dataStructureEntit);
        
        dataCorrosionEntit.setDId(dId);
        dataCorrosionEntit.setCode(dataBase.getCode());
        dataCorrosionService.updateById(dataCorrosionEntit);

        return R.ok();
    }

    /**
     * 删除
     */
    @PostMapping("/delete")
    @RequiresPermissions("admin:data:delete")
    public R delete(@RequestBody Long[] ids){
        dataBaseService.removeByIds(Arrays.asList(ids));
        
        return R.ok();
    }

}
