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

import com.github.pagehelper.Page;
import com.jpsoft.shinestar.modules.common.utils.PojoUtils;
import com.jpsoft.shinestar.modules.common.dto.Sort;
import com.jpsoft.shinestar.modules.common.dto.MessageResult;
import com.jpsoft.shinestar.modules.base.entity.CompanyPosition;
import com.jpsoft.shinestar.modules.base.service.CompanyPositionService;
import com.jpsoft.shinestar.modules.sys.entity.User;
import com.jpsoft.shinestar.modules.sys.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
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/companyPosition")
@Api(description = "companyPosition")
public class CompanyPositionController {
    private Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private CompanyPositionService companyPositionService;
    @Autowired
    private UserService userService;

    @ApiOperation(value="保存信息")
    @PostMapping("save")
    public MessageResult<CompanyPosition> save(@RequestBody CompanyPosition companyPosition,@RequestAttribute String subject){
        User user = userService.get(subject);
        MessageResult<CompanyPosition> msgResult = new MessageResult<>();

        try {
            int affectCount = 0;
            if(StringUtils.isEmpty(companyPosition.getId())) {
                companyPosition.setCompanyId(user.getCompanyId());
                companyPosition.setDelFlag(false);
                companyPosition.setCreateBy(subject);
                companyPosition.setCreateTime(new Date());

                affectCount = companyPositionService.insert(companyPosition);
            }else{
                companyPosition.setUpdateBy(subject);
                companyPosition.setUpdateTime(new Date());

                affectCount = companyPositionService.update(companyPosition);
            }

            if (affectCount > 0) {
                msgResult.setResult(true);
                msgResult.setData(companyPosition);
            }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("detail/{id}")
    public MessageResult<CompanyPosition> detail(@PathVariable("id") String id){
        MessageResult<CompanyPosition> msgResult = new MessageResult<>();

        try {
            CompanyPosition companyPosition = companyPositionService.get(id);

            if (companyPosition != null) {
                msgResult.setResult(true);
                msgResult.setData(companyPosition);
            } 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 {
            CompanyPosition companyPosition = companyPositionService.get(id);
            companyPosition.setDelFlag(true);
            companyPosition.setUpdateBy(subject);
            companyPosition.setUpdateTime(new Date());

            int affectCount = companyPositionService.update(companyPosition);

            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) {
                CompanyPosition companyPosition = companyPositionService.get(id);
                companyPosition.setDelFlag(true);
                companyPosition.setUpdateBy(subject);
                companyPosition.setUpdateTime(new Date());

                affectCount += companyPositionService.update(companyPosition);
            }

            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)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "companyId",value = "公司编号", required = false, paramType = "form",dataType = "String"),
    })
    public MessageResult<Map> pageList(
            @RequestParam(value="companyId",defaultValue="") String companyId,
            @RequestParam(value="pageIndex",defaultValue="1") int pageIndex,
            @RequestParam(value="pageSize",defaultValue="20") int pageSize,
            @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("id_","asc"));

        if (StringUtils.isNotEmpty(companyId)) {
            searchParams.put("companyId",companyId);
        }

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

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

        return msgResult;
    }

    @ApiOperation(value="根据公司获取详情信息")
    @GetMapping("detailForCompany")
    public MessageResult<CompanyPosition> detailForCompany(@RequestAttribute String subject){
        User user = userService.get(subject);
        MessageResult<CompanyPosition> msgResult = new MessageResult<>();

        try {
            Map<String,Object> searchParams = new HashMap<>();
            searchParams.put("companyId",user.getCompanyId());

            List<Sort> sortList = new ArrayList<>();
            sortList.add(new Sort("id_","asc"));

            CompanyPosition companyPosition = new CompanyPosition();
            Page<CompanyPosition> page = companyPositionService.pageSearch(searchParams,1,1,false,sortList);

            if (page.size() > 0) {
                companyPosition = page.get(0);
            }

            msgResult.setResult(true);
            msgResult.setData(companyPosition);
        }
        catch(Exception ex){
            logger.error(ex.getMessage(),ex);

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

        return msgResult;
    }
}
