package com.jpsoft.shinestar.modules.base.controller;

import com.github.pagehelper.Page;
import com.jpsoft.shinestar.modules.base.entity.OwnerInfo;
import com.jpsoft.shinestar.modules.base.service.OwnerInfoService;
import com.jpsoft.shinestar.modules.common.dto.MessageResult;
import com.jpsoft.shinestar.modules.common.dto.Sort;
import com.jpsoft.shinestar.modules.common.utils.PojoUtils;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;

@RestController
@RequestMapping("/base/ownerInfo")
public class OwnerInfoController {
    private Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private OwnerInfoService ownerInfoService;

    @ApiOperation(value="创建空记录")
    @GetMapping("create")
    public MessageResult<OwnerInfo> create(){
        MessageResult<OwnerInfo> msgResult = new MessageResult<>();
        OwnerInfo ownerInfo = new OwnerInfo();

        msgResult.setData(ownerInfo);
        msgResult.setResult(true);

        return msgResult;
    }
    
    @ApiOperation(value="添加信息")
    @PostMapping("add")
    public MessageResult<OwnerInfo> add(@RequestBody OwnerInfo ownerInfo,@RequestAttribute String subject){
        MessageResult<OwnerInfo> msgResult = new MessageResult<>();

        try {
            ownerInfo.setId(UUID.randomUUID().toString());
            ownerInfo.setDelFlag(false);
            ownerInfo.setCreateBy(subject);
            ownerInfo.setCreateTime(new Date());
            
            int affectCount = ownerInfoService.insert(ownerInfo);

            if (affectCount > 0) {
                msgResult.setResult(true);
                msgResult.setData(ownerInfo);
            } else {
                msgResult.setResult(false);
                msgResult.setMessage("数据库添加失败");
            }
        }
        catch(Exception ex){
            logger.error(ex.getMessage(),ex);

            msgResult.setResult(false);
            msgResult.setMessage(ex.getMessage());
        }

        return msgResult;
    }

    @ApiOperation(value="获取信息")
    @GetMapping("edit/{id}")
    public MessageResult<OwnerInfo> edit(@PathVariable("id") String id){
        MessageResult<OwnerInfo> msgResult = new MessageResult<>();

        try {
            OwnerInfo ownerInfo = ownerInfoService.get(id);

            if (ownerInfo != null) {
                msgResult.setResult(true);
                msgResult.setData(ownerInfo);
            } else {
                msgResult.setResult(false);
                msgResult.setMessage("数据库不存在该记录!");
            }
        }
        catch(Exception ex){
            logger.error(ex.getMessage(),ex);

            msgResult.setResult(false);
            msgResult.setMessage(ex.getMessage());
        }

        return msgResult;
    }

    @ApiOperation(value="更新用户")
    @PostMapping("update")
    public MessageResult<OwnerInfo> update(@RequestBody OwnerInfo ownerInfo,@RequestAttribute String subject){
        MessageResult<OwnerInfo> msgResult = new MessageResult<>();

        try {
            ownerInfo.setUpdateBy(subject);
            ownerInfo.setUpdateTime(new Date());
            
            int affectCount = ownerInfoService.update(ownerInfo);

            if (affectCount > 0) {
                msgResult.setResult(true);
                msgResult.setData(ownerInfo);
            } else {
                msgResult.setResult(false);
                msgResult.setMessage("数据库更新失败");
            }
        }
        catch(Exception ex){
            logger.error(ex.getMessage(),ex);

            msgResult.setResult(false);
            msgResult.setMessage(ex.getMessage());
        }

        return msgResult;
    }

	@ApiOperation(value="删除")
    @PostMapping("delete/{id}")
    public MessageResult<Integer> delete(@PathVariable("id") String id,@RequestAttribute String subject){
        MessageResult<Integer> msgResult = new MessageResult<>();

        try {
            OwnerInfo ownerInfo = ownerInfoService.get(id);
            ownerInfo.setDelFlag(true);
            ownerInfo.setUpdateBy(subject);
            ownerInfo.setUpdateTime(new Date());

            int affectCount = ownerInfoService.update(ownerInfo);

            if (affectCount > 0) {
                msgResult.setResult(true);
                msgResult.setData(affectCount);
            } else {
                msgResult.setResult(false);
                msgResult.setMessage("删除失败");
            }
        }
        catch(Exception ex){
            logger.error(ex.getMessage(),ex);

            msgResult.setResult(false);
            msgResult.setMessage(ex.getMessage());
        }

        return msgResult;
    }


    @ApiOperation(value="批量删除")
    @PostMapping("batchDelete")
    public MessageResult<Integer> batchDelete(@RequestBody List<String> idList,@RequestAttribute String subject){
        MessageResult<Integer> msgResult = new MessageResult<>();

        try {
            int affectCount = 0;

            for (String id : idList) {
                OwnerInfo ownerInfo = ownerInfoService.get(id);
                ownerInfo.setDelFlag(true);
                ownerInfo.setUpdateBy(subject);
                ownerInfo.setUpdateTime(new Date());

                affectCount += ownerInfoService.update(ownerInfo);
            }

            if (affectCount > 0) {
                msgResult.setResult(true);
                msgResult.setData(affectCount);
            } else {
                msgResult.setResult(false);
                msgResult.setMessage("删除失败");
            }
        }
        catch(Exception ex){
            logger.error(ex.getMessage(),ex);

            msgResult.setResult(false);
            msgResult.setMessage(ex.getMessage());
        }

        return msgResult;
    }

    @ApiOperation(value="列表")
    @RequestMapping(value = "pageList",method = RequestMethod.POST)
    public MessageResult<Map> pageList(
            String id,
            @RequestParam(value="draw",defaultValue="1") int draw,
            @RequestParam(value="start",defaultValue="1") int start,
            @RequestParam(value="length",defaultValue="20") int length,
            @RequestAttribute String subject){

        //当前用户ID
        System.out.println(subject);
        MessageResult<Map> msgResult = new MessageResult<>();

        Map<String,Object> searchParams = new HashMap<>();

        List<Sort> sortList = new ArrayList<>();
        sortList.add(new Sort("create_time","desc"));

        if (StringUtils.isNotEmpty(id)) {
            searchParams.put("id","%" + id + "%");
        }

        int pageNum = start / length;

        if (start % length != 0) {
            pageNum++;
        }

        int pageSize = length;

        Page<OwnerInfo> page = ownerInfoService.pageSearch(searchParams,pageNum,pageSize,sortList);

        msgResult.setResult(true);
        msgResult.setData(PojoUtils.pageWrapper(page));

        return msgResult;
    }
}
