package com.jpsoft.shinestar.modules.workflow;

import com.github.pagehelper.Page;
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 com.jpsoft.shinestar.modules.workflow.entity.ProcessSet;
import com.jpsoft.shinestar.modules.workflow.service.ProcessSetService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.sf.json.JSONObject;
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 javax.servlet.http.HttpServletRequest;
import java.util.*;

@RestController
@RequestMapping("/workflow/processSet")
@Api(description = "processSet")
public class ProcessSetController {
    private Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private ProcessSetService processSetService;

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

        ProcessSet processSet = new ProcessSet();

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

        return msgResult;
    }

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

        try {
            processSet.setId(UUID.randomUUID().toString());
            processSet.setCreateBy(subject);
            processSet.setCreateTime(new Date());
            processSet.setDelFlag(false);

            int affectCount = processSetService.insert(processSet);

            if (affectCount > 0) {
                msgResult.setResult(true);
                msgResult.setData(processSet);
            } 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<ProcessSet> edit(@PathVariable("id") String id){
        MessageResult<ProcessSet> msgResult = new MessageResult<>();

        try {
            ProcessSet processSet = processSetService.get(id);

            if (processSet != null) {
                msgResult.setResult(true);
                msgResult.setData(processSet);
            } 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<Integer> update(@RequestBody ProcessSet processSet,@RequestAttribute String subject){
        MessageResult<Integer> msgResult = new MessageResult<>();

        try {
            processSet.setUpdateBy(subject);
            processSet.setUpdateTime(new Date());

            int affectCount = processSetService.update(processSet);

            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("delete/{id}")
    public MessageResult<ProcessSet> delete(@PathVariable("id") String id,@RequestAttribute String subject){
        MessageResult<ProcessSet> msgResult = new MessageResult<>();

        try {
            ProcessSet processSet = processSetService.get(id);

            processSet.setUpdateBy(subject);
            processSet.setUpdateTime(new Date());
            processSet.setDelFlag(true);

            int affectCount = processSetService.update(processSet);

            if (affectCount > 0) {
                msgResult.setResult(true);
            } 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) {
                ProcessSet processSet = processSetService.get(id);
                processSet.setDelFlag(true);
                processSet.setUpdateBy(subject);
                processSet.setUpdateTime(new Date());

                affectCount += processSetService.update(processSet);
            }

            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 name,
            @RequestParam(value="pageIndex",defaultValue="1") int pageIndex,
            @RequestParam(value="pageSize",defaultValue="20") int pageSize,
            HttpServletRequest request){
        String subject = (String)request.getAttribute("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(name)) {
            searchParams.put("name","%" + name + "%");
        }

        Page<ProcessSet> page = processSetService.pageSearch(searchParams,pageIndex,pageSize,true,sortList);

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

        return msgResult;
    }


    @ApiOperation(value="更新状态")
    @PostMapping("updateStatus")
    public MessageResult<Integer> updateStatus(String id,boolean status,@RequestAttribute String subject){
        MessageResult<Integer> msgResult = new MessageResult<>();

        try {
            ProcessSet processSet = processSetService.get(id);
            processSet.setStatus(status);
            processSet.setUpdateBy(subject);
            processSet.setUpdateTime(new Date());

            int affectCount = processSetService.update(processSet);

            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 = "list",method = RequestMethod.POST)
    public MessageResult<List<ProcessSet>> list(){

        MessageResult<List<ProcessSet>> msgResult = new MessageResult<>();


        List<ProcessSet> list = processSetService.list();

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

        return msgResult;
    }
}
